LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2004, 03:09 PM   #1
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Rep: Reputation: 15
FIFO using pointers


Can someone show me show me how to create a basic First in First out (FIFO) waiting line using pointers? It needs to be dynamic because I don't know how many users are going to go into the FIFO line. I wrote one a long time ago for a program and I'm so rusty with pointers that I can't remember how do it. Thanks a lot!!

-AquamaN
 
Old 03-21-2004, 05:15 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
You're going to have a structure like
Code:
struct fifo_element {
    struct fifo_element *next;
    void *data;
};
Then you can represent the whole FIFO as

Code:
struct fifo {
    struct fifo_element *head;
    struct fifo_element *tail;
};
To add a fifo_element *foo to a fifo bar,
Code:
struct fifo_element *foo = malloc(sizeof(*foo));
if (foo == NULL)
    barf();
foo->next = NULL;
foo->data = my_important_data;
bar.tail->next = foo;
bar.tail = foo;
To pop the first fifo_element from bar,

Code:
struct fifo_element *old = bar.head;
bar.head = bar.head->next;
free_fifo_element(old);
There are special cases when the fifo is empty, since you should have head and tail == NULL, and of course you don't want to dereference that. I leave those to you
 
Old 03-21-2004, 05:51 PM   #3
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Original Poster
Rep: Reputation: 15
Thanks a lot!!! You've helped me remember A LOT. Thanks once again!

-AquamaN
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ST7554: fifo underrun! Spriggan Linux - Hardware 2 03-01-2005 04:02 AM
snmptrapd FIFO eccsamba Linux - Networking 2 11-24-2004 08:52 AM
Non-blocking write to a fifo droussel Programming 0 05-22-2003 01:12 PM
FIFO question gauge73 Linux - Newbie 1 02-28-2003 11:39 PM
nfs FIFO??? granny Linux - Newbie 1 12-16-2002 03:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration