Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,232 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,811 people online right now. Registration is fast and FREE... Join Now!




Circular Queue

 
Reply to this topicStart new topic

Circular Queue

lavender
20 Mar, 2007 - 05:17 AM
Post #1

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 4


My Contributions
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:
CODE

#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;
        
    }
}


User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Circular Queue
20 Mar, 2007 - 05:43 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,013



Thanked: 18 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
I don't know what you're expecting to happen?
If the queue holds 10 items it will stop accepting after 10 items. the idea behind a circular queue is that if for some reason it is starting at element other than the last, when releasing the queue, it can add an element in the free position since the queue wraps around on itself.
User is offlineProfile CardPM
+Quote Post

illello
RE: Circular Queue
20 Mar, 2007 - 07:02 AM
Post #3

New D.I.C Head
*

Joined: 9 Sep, 2006
Posts: 7


My Contributions
The problem is here

CODE

BOOL fullQueue(queue *q)
{
    return ((BOOL)(((q->tail+1) % maxQueue) == q->head));
}


When you will have 9 elements in your queue, the function will return always TRUE.

In my opinion you have to change the fullqueue function in this way
CODE

BOOL fullQueue(queue *q)
{
    
    return ((BOOL)(q->tail-maxQueue) == q->head);    

}


And change also the function insertqueue
CODE

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); // Changed here
    
        
    }
}









User is offlineProfile CardPM
+Quote Post

lavender
RE: Circular Queue
20 Mar, 2007 - 07:08 AM
Post #4

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 4


My Contributions
Thanks illello, it really works....
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:30PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month