Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help with linked list for displaying "queue is full" (http://www.go4expert.com/forums/help-linked-list-displaying-queue-t23974/)

sujeshshetty 24Nov2010 05:48

Help with linked list for displaying "queue is full"
 
1 Attachment(s)
How to show that the queue is full / queue underflow using linked list , i mean what are the basic steps to make the program to display it ?? can anyone help me to put the "stack is full" option under the insert function in this given program?? i wan't to add only that function !! i tried my best but didn't find any proper solution ! any help would mean a lot to me !!
program is given below , i just wan't to add the "queue is full" option !! thanx a lot in advance
program !! Attachment 802

xpi0t0s 24Nov2010 13:28

Re: Help with linked list for displaying "queue is full"
 
Queue full and queue underflow are two different conditions and you cannot check for them both at the same time (well, you could, but there's not a lot of point. There's no need to check if the queue is about to underflow when you add an item, or if the queue is about to overflow when you delete an item).

Linked lists generally only need to run out when memory runs out so a queue full should normally correspond to a check for failure to allocate memory, but the error then should be "not enough memory", not "queue full". However there could be design reasons for your queue being limited in size (but then, why are you using a linked list instead of a simple array?).

Anyway, it's not difficult. Just check if the queue is at maximum size and throw an error when the user tries to add more:
Code:

void insert(item *i)
{
  if (queue_size < max_size)
  {
    // .. as per your existing code, and don't forget of course:
    queue_size++;
  }
  else
  {
    printf("Queue is full\n");
  }
}

Queue underflow is very similar, except I'm going to make you guess what the comparison should be:
Code:

void remove(item *i)
{
  if (/* what goes here? */)
  {
    // .. as per your existing code, again not forgetting:
    queue_size--;
  }
  else
  {
    printf("Queue underflow\n");
  }
}

If you can't guess, think about it for a bit. What are the range of possible values queue_size could have if the queue is about to underflow?


All times are GMT +5.5. The time now is 09:26.