473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Circular queue

When define a maxQueue is 10, means it able to store 10 items in circular queue,but when I key in the 10 items, it show "Queue Full" in items number 10.
Where is the wrong in my code? Why it cannot store up to 10 items?

Output from my code:

Enter you choice: 1
Enter ID document to print : 21

Enter you choice: 1
Enter ID document to print : 22

Enter you choice: 1
Enter ID document to print : 23

Enter you choice: 1
Enter ID document to print : 24

Enter you choice: 1
Enter ID document to print : 25

Enter you choice: 1
Enter ID document to print : 26

Enter you choice: 1
Enter ID document to print : 27

Enter you choice: 1
Enter ID document to print : 28

Enter you choice: 1
Enter ID document to print : 29

Enter you choice: 1
Enter ID document to print : 30
Error: Queue Full




Here is my code;
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define maxQueue 10
  3.  
  4. typedef char BOOL;
  5. typedef int itemType;
  6. typedef struct QUEUE
  7. {
  8.     int head;
  9.     int tail;
  10.     int items[maxQueue];
  11. }queue;
  12.  
  13. void initQueue(queue *);
  14. BOOL emptyQueue(queue *);
  15. BOOL fullQueue(queue *);
  16. void insertQueue(itemType ,queue *);
  17. void deleteQueue(itemType *,queue *);
  18. void menu();
  19. void SendToPrint(queue *);
  20. void Print(queue *);
  21.  
  22.  
  23. int main()
  24. {
  25.     queue nQ;
  26.     int i = 0;
  27.     //clrscr();
  28.     initQueue(&nQ);
  29.     do
  30.     {
  31.         menu();
  32.         scanf("%d",&i);
  33.         switch (i)
  34.         {
  35.             case 1:SendToPrint(&nQ);break;
  36.  
  37.             case 2:Print(&nQ);break;
  38.  
  39.             case 3:Exit(0);break;
  40.  
  41.             default:printf("\nERROR");break;
  42.         }
  43.     }while (i!=3);
  44. }
  45.  
  46. void SendToPrint(queue *q)
  47. {
  48.     itemType nItem;
  49.     printf("\n\nEnter ID document to print :");
  50.     scanf("\t%d",&nItem);
  51.     insertQueue(nItem,q);
  52.  
  53.     return;
  54. }
  55.  
  56. void Print(queue *q)
  57. {
  58.     int i;
  59.     BOOL state;
  60.     printf("\n\nCheck printer buffer...");
  61.  
  62.  
  63.     if(q->tail < q->head) 
  64.         state = ((BOOL)(q->head - q->tail) > 4);
  65.     else
  66.         state = ((BOOL)(q->tail - q->head) > 4);
  67.     if (state)
  68.     {
  69.         for (i = 0;i<5 ;i++)
  70.         {
  71.             printf("\nID Document print  %d",q->items[q->head]);
  72.             deleteQueue(q->items[q->head],q);
  73.  
  74.         }
  75.     }
  76.     else
  77.         printf("\nError Printer buffer no enough\n");
  78.     return;
  79. }
  80.  
  81. void menu()
  82. {
  83.     printf("\n****Printer Spooling****\n");
  84.     printf("1 - Sent document to print\n");
  85.     printf("2 - Print document\n");
  86.     printf("3 - Exit\n");
  87.     printf("Please enter your choice :");
  88. }
  89.  
  90. void initQueue(queue *q)
  91. {
  92.    q->head = 0;
  93.    q->tail = 0;
  94. }
  95.  
  96. BOOL emptyQueue(queue *q)
  97. {
  98.     return ((BOOL)(q->head == q->tail));
  99. }
  100.  
  101. BOOL fullQueue(queue *q)
  102. {
  103.     return ((BOOL)(((q->tail-1) % maxQueue) == q->head));
  104.  
  105. }
  106.  
  107. void insertQueue(itemType nItem,queue *q)
  108. {
  109.     if (fullQueue(q))
  110.         printf(" Error : Queue Full\n");
  111.     else
  112.     {
  113.  
  114.             q->items[q->tail] = nItem;
  115.             q->tail = (q->tail+1)%maxQueue;
  116.  
  117.  
  118.     }
  119. }
  120.  
  121. void deleteQueue(itemType *nItem,queue *q)
  122. {
  123.     if (emptyQueue(q))
  124.         printf(" Error : Queue Empty\n");
  125.     else
  126.     {
  127.  
  128.         q->head = (q->head + 1) % maxQueue;
  129.  
  130.     }
  131. }
Mar 20 '07 #1
2 2938
DeMan
1,806 1GB
Have you tried setting up a function to display eveery item in the queue? This is always a very good way to see if something fishy is going on
Mar 20 '07 #2
Banfa
9,065 Expert Mod 8TB
The way you have set up your circular buffer (which is fairly common as it gets round some other problems mainly distinguishing between an empty list and a full list) you have to have at least 1 empty entry in the list. So if the list is 10 items long it can only hold 9 items in it.

This comes from the maths in your fullQueue function, the queue is full if the tail is 1 item ahead of the head, since the tail points to the next item to write and not a filled in item it must therefore point to an empty slot so your queue must contain 1 unused slot.

It you changed the queue full definition by removing the -1 from fullQueue then you would find the maths in fullQueue resolve to exactly the same test as emptyQueue, you would not be able to distinguish between an empty queue and a full queue.

Often it is just accepted that the queue holds size-1 items as it makes the code a lot simpler for not too much memory overhead, especially since often if you are using a circular buffer you do not want to get to the situation of having a full queue anyway as this is often some sort of error.
Mar 20 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Zhaozhi Gao | last post by:
I'm doing a simple project for school. This is the first time I've ever used Access. Is there a way i can simulate a Circular Queue data structure for the datas in a table. I tried assign index...
6
by: herrcho | last post by:
#include <stdio.h> #define MAX 10 int queue; int front, rear; void init_queue() { front=rear=0; }
2
by: Bob Jenkins | last post by:
I have this cute data structure that I just found a second opportunity to use. I googled for references to it, but came up empty. I probably just don't know its name. The algorithm on this...
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
2
by: William Stacey | last post by:
Working with implementing a circular buffer for a producer/consumer deal. Have not done one in a while. The following seems to work. However, I remember and have seen other implementation that...
10
by: avsrk | last post by:
Hi Folks I want to create a circular queue for high speed data acquistion on QNX with GNU C++ . Does any one know of efficient ways either Using STL or in any other way . I am thing of ...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
2
by: marco.furlan | last post by:
Hi there, I have to write an optimized circular buffer to log events in C++. The elements are of type std::string. This should run under Linux on an ARM embedded system. I can dedicate to this...
5
by: shanknbake | last post by:
Here is my code. I've noted where the program crashes. I'm doing this program as a project for school. //cqueue.h file //HEADER FILE http://rafb.net/paste/results/Nh0aLB77.html...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.