473,836 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Circular queue

12 New Member
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
  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;
  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 *);
  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;
  37.             case 2:Print(&nQ);break;
  39.             case 3:Exit(0);break;
  41.             default:printf("\nERROR");break;
  42.         }
  43.     }while (i!=3);
  44. }
  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);
  53.     return;
  54. }
  56. void Print(queue *q)
  57. {
  58.     int i;
  59.     BOOL state;
  60.     printf("\n\nCheck printer buffer...");
  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);
  74.         }
  75.     }
  76.     else
  77.         printf("\nError Printer buffer no enough\n");
  78.     return;
  79. }
  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. }
  90. void initQueue(queue *q)
  91. {
  92.    q->head = 0;
  93.    q->tail = 0;
  94. }
  96. BOOL emptyQueue(queue *q)
  97. {
  98.     return ((BOOL)(q->head == q->tail));
  99. }
  101. BOOL fullQueue(queue *q)
  102. {
  103.     return ((BOOL)(((q->tail-1) % maxQueue) == q->head));
  105. }
  107. void insertQueue(itemType nItem,queue *q)
  108. {
  109.     if (fullQueue(q))
  110.         printf(" Error : Queue Full\n");
  111.     else
  112.     {
  114.             q->items[q->tail] = nItem;
  115.             q->tail = (q->tail+1)%maxQueue;
  118.     }
  119. }
  121. void deleteQueue(itemType *nItem,queue *q)
  122. {
  123.     if (emptyQueue(q))
  124.         printf(" Error : Queue Empty\n");
  125.     else
  126.     {
  128.         q->head = (q->head + 1) % maxQueue;
  130.     }
  131. }
Mar 20 '07 #1
2 2969
1,806 Top Contributor
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
9,065 Recognized Expert Moderator Expert
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

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 and use sorting, but the problem is that i will have new data enqueued very often, and the sorting will not simulate the CIRCULAR Queue. Can any one suggest a way of doing it with query, VB, or SQL?
by: herrcho | last post by:
#include <stdio.h> #define MAX 10 int queue; int front, rear; void init_queue() { front=rear=0; }
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 data structure typically has an array of pointers, and in a loop refers to array .. array, modifies array, then increments i. I do away with i by having an array of size 2*n, where array = array. Then I use array2 instead of array, increment...
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 stumped again with this little love triangle. Here is some background: m_commands.h defines - struct command_callbacks, which contains - a struct conn * reference - struct command, which contains - a struct command_callback reference
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 make the buffer 1 more then the needed capacity so that a full buffer and an empty buffer can be differentiated. However this implementation does not do that (i.e. the capacity and the buffer size is the same.) I assume the following does not...
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 using queue or dqueue to implement it ... any good info is appreciated . I want it to be efficient , high speed for developing device drivers .
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 cost. STL vector is suitable for removing form tail? or it is as costly as removing from middle? any other std container to serve this purpose? (note , I dont need linked list implementation of any container, as I want random access)
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 circular log a preallocated block of fixed size and work in there. Can anybody indicate me an example or technique to do this ? Thanks Marco
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 -------------------------------------------------------------------- //cqueue.cpp file //IMPLEMENTATION OF cqueue CLASS http://rafb.net/paste/results/A2gXAr73.html
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.