Solved

implementing a queue of structure

Posted on 1998-08-15
6
254 Views
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.
0
Comment
Question by:zigla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Author Comment

by:zigla
ID: 1252128
Edited text of question
0
 
LVL 1

Expert Comment

by:TheMadManiac
ID: 1252129
what does the menu have to do with the fifo queue ?
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1252130
Create a struct something like this

typedef tagMYSTRUCT
{
struct tagMYSTRUCT * pNext ; /* pointer to next element */
/* Your data goes here */
char exampledata ;
} MYSTRUCT ;

You also need a variable which points to the top element of the queue, e.g.
MYSTRUCT * pTopElement = NULL ;

To add element to queue do this

int AddItem( char data ) /* return 0 for success, -1 for error */
{
 /* Allocate new item */
 MYSTRUCT * newitem = (MYSTRUCT *)malloc( sizeof(MYSTRUCT) ) ;

 /* malloc could fail */
 if ( newitem == NULL ) return -1 ;

 /* if queue is previously empty then this is top element */
 if ( pTopElement == NULL )
 {
    pTopElement = newitem ;
    newitem->exampledata = data ;
    newitem->pNext = NULL ; /* Last item in queue */
    return 0;
 }

 newitem->pNext = pTopElement ;
 newitem->exampledata = data ;
 pTopElement = newitem ;
 return 0 ;
}

To remove an item

int PopItem( char * pdata ) /* return 0 for success, -1 for error */
{
   MYSTRUCT * pTemp ;

   if ( pTopElement == NULL )
   {
      /* Nothing in queue, return error */
      return -1 ;
   }

   /* Get data from top item */
   *pdata = pTopElement->exampledata ;

   /* Free top item, and make 2nd from top the top */
   pTemp = pTopElement->pNext ;
   free(pTopElement) ;
   pTopElement = pTemp ;

   return 0 ;

}

To clean up when you're finished completely with the queue
void FreeQueue( void )
{
  char cDummy ;

  while ( pTopElement != NULL )
  {
       PopItem( &cDummy ) ;
  }
}

Replace the example data (in my case a character) with the data you really need.  If you want each item to have a block of data I suggest you memcpy into the structure.

That should be enough to get you started (sorry for any typos above, but the algorithm should be there for you).

And don't forget to check the return values from AddItem and PopItem, because they can fail.  AddItem will fail if you run out of memory.  PopItem will fail if you calling code attempts to pull more stuff out the queue than you've added to it.

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 5

Expert Comment

by:ecw
ID: 1252131
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;
}

0
 
LVL 1

Expert Comment

by:newexpert
ID: 1252132
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?
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1252133
OOps ecw is right, misread the question.  Not tried his code
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question