We help IT Professionals succeed at work.

implementing a queue of structure

zigla asked
Medium Priority
Last Modified: 2010-04-15
i would like to get help on how to implement a FIFO queue of data structures using c. i would like to use it as an entry point in my program i am currently coding for tcp/ip routing program. It should have a menu.I would appreciate if i got help soon .I am a student at the university.
Watch Question


Edited text of question
what does the menu have to do with the fifo queue ?
Unlock this solution and get a sample of our free trial.
(No credit card required)

Answers2000.  You've implemented a LIFO.  You're adding to the front of the list and removing from the front.  Try something like:

#include <assert.h>
#include <malloc.h>

struct queue_item {
  void *data;
  struct queue_item *next;

typedef struct queue *Queue;
struct queue {
  struct queue_item *head;
  struct queue_item *last;
} ;

Queue Queue_create(void)
Queue queue = calloc((size_t) 1, sizeof(*queue));
  return queue;

void *Queue_add_item(Queue queue, void *data)
struct queue_item *item;

  assert(queue != 0);

  if ((item = malloc(sizeof(*item)))) {
   item-> data = data;
   item-> next = 0;
   if (! queue-> head) {
     queue-> head = queue-> last = item;
   } else {
     queue-> last = queue-> last-> next = item;
  return item;

void *Queue_get_head(Queue queue)
struct queue_item *next = 0;
void *data = 0;
  assert(queue != 0);
  if (queue-> head) {
    data = queue-> head-> data;
    next = queue-> head-> next;
    free(queue-> head);
    if (! (queue-> head = next)) {
      queue-> last = 0;
  return data;

Zigla: I don't see how a menu relates to a FIFO queue though.  Can you elaborate on the problem a little bit more, please?
OOps ecw is right, misread the question.  Not tried his code
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.