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
- #include <stdio.h>
- #define maxQueue 10
- typedef char BOOL;
- typedef int itemType;
- typedef struct QUEUE
- {
- int head;
- int tail;
- int items[maxQueue];
- }queue;
- void initQueue(queue *);
- BOOL emptyQueue(queue *);
- BOOL fullQueue(queue *);
- void insertQueue(itemType ,queue *);
- void deleteQueue(itemType *,queue *);
- void menu();
- void SendToPrint(queue *);
- void Print(queue *);
- int main()
- {
- queue nQ;
- int i = 0;
- //clrscr();
- initQueue(&nQ);
- do
- {
- menu();
- scanf("%d",&i);
- switch (i)
- {
- case 1:SendToPrint(&nQ);break;
- case 2:Print(&nQ);break;
- case 3:Exit(0);break;
- default:printf("\nERROR");break;
- }
- }while (i!=3);
- }
- void SendToPrint(queue *q)
- {
- itemType nItem;
- printf("\n\nEnter ID document to print :");
- scanf("\t%d",&nItem);
- insertQueue(nItem,q);
- return;
- }
- void Print(queue *q)
- {
- int i;
- BOOL state;
- printf("\n\nCheck printer buffer...");
- if(q->tail < q->head)
- state = ((BOOL)(q->head - q->tail) > 4);
- else
- state = ((BOOL)(q->tail - q->head) > 4);
- if (state)
- {
- for (i = 0;i<5 ;i++)
- {
- printf("\nID Document print %d",q->items[q->head]);
- deleteQueue(q->items[q->head],q);
- }
- }
- else
- printf("\nError Printer buffer no enough\n");
- return;
- }
- void menu()
- {
- printf("\n****Printer Spooling****\n");
- printf("1 - Sent document to print\n");
- printf("2 - Print document\n");
- printf("3 - Exit\n");
- printf("Please enter your choice :");
- }
- void initQueue(queue *q)
- {
- q->head = 0;
- q->tail = 0;
- }
- BOOL emptyQueue(queue *q)
- {
- return ((BOOL)(q->head == q->tail));
- }
- BOOL fullQueue(queue *q)
- {
- return ((BOOL)(((q->tail-1) % maxQueue) == q->head));
- }
- void insertQueue(itemType nItem,queue *q)
- {
- if (fullQueue(q))
- printf(" Error : Queue Full\n");
- else
- {
- q->items[q->tail] = nItem;
- q->tail = (q->tail+1)%maxQueue;
- }
- }
- void deleteQueue(itemType *nItem,queue *q)
- {
- if (emptyQueue(q))
- printf(" Error : Queue Empty\n");
- else
- {
- q->head = (q->head + 1) % maxQueue;
- }
- }