Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is there anything wrong with this implementation of Queue with array?

I have started front = rear = -1.

Some references tend to start front = 0 like this:enter link description here

I think if front starts with 0, the source code becomes cumbersome.

#include <stdio.h>
#include<ctype.h>
#define SIZE 5

int queue[SIZE] = {0};
int front = -1;
int rear = -1;

void PrintQueue()
{
    int i=0;

    if(!IsEmpty())
    {
        for(i=front+1 ; i<=rear ; i++)
        {
            printf("%d, ", queue[i]);
        }
        printf("\n");
    }
    else
    {
        printf("Queue is empty.\n");
    }
}

int IsEmpty()
{
    if(front==rear)
    {
        front = rear = -1;
        return 1;
    }
    else
    {
        return 0;
    }
}

void Enque(int value)
{
    if(rear<SIZE-1)
    {
        ++rear;
        queue[rear] = value;
    }
    else
    {
        printf("Queue overflow!\n");
    }
}

int Dequeue()
{
    int ret = 0;
    if(!IsEmpty())
    {
        ++front;
        ret = queue[front];
    }
    else
    {
        printf("Queue underflow!\n");
        ret = -99;
    }

    return ret;
}

main()
{
    int value = 0;
    int menu = 0;

    while(1)
    {
        printf("Menu\n");
        printf("--------\n");
        printf("1. Enque\n");
        printf("2. Deque\n");
        printf("3. IsEmpty\n");
        printf("4. Print\n");
        printf("5. Clrscr\n");
        printf("6. Exit\n");
        scanf(" %d", &menu);

        switch(menu)
        {
        case 1:
            printf("Enter an element : ");
            scanf(" %d", &value);
            Enque(value);
            break;

        case 2:
            printf("Dequed, %d.\n", Dequeue());
            break;

        case 3:
            if(IsEmpty())printf("Queue is empty.\n");
            else printf("Que has some values.\n");
            break;

        case 4:
            PrintQueue();
            break;

        case 5:
            system("cls");
            break;

        case 6:
            exit(0);
            break;
        }
    }
}
share|improve this question
1  
Enqueue and Dequeue –  Snowbear Jun 6 '11 at 9:05
 
COULD NOT UNDERSTAND! –  BROY Jun 6 '11 at 13:54
add comment

1 Answer

up vote 2 down vote accepted

Most implementations of a Queue use front to point to the head element of the queue and rear to point to the first empty element at the rear of the queue:

front       rear
v            v
[1][2][3][4][ ][ ]

Your implementation has them pointing one element previous.

To change your code to work like this would be a matter of change front/rear to start at 0 and then reversing your increment/read:

void Enqueue(int value)
{
    if(rear<SIZE-1)
    {
        queue[rear] = value;
        ++rear;
    }
    else
    {
        printf("Queue overflow!\n");
    }
}

Review:

Your IsEmpty function has the side effect of resetting the queue. The name does not reflect this; it sounds like it should be merely checking whether the queue is empty. There's not really anything wrong with that. Pragmatically, such an operation should be transparent. However, I'd put a comment in there describing the side effect.

You could also implement a circularly linked queue in which you insert elements in a (conceptual) ring. Basically, when you get to the end of the array, you start inserting at the begining again. However, this means that when front = back the buffer could be full or empty. Just check rear + 1 to see if it's empty.

Oh, and please spell "queue" and its varients correctly...it's really bugging me :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.