Friday 2 September 2016

Circular Queue Using an Array

Circular Queue Using an Array

#include <stdio.h>
#include<stdlib.h>
#define MAX 3

char cq[MAX];
int front = -1,rear = -1;
void add(char);
void del();
void display();

int main()
{
            int ch;
            char item;
            while(1)
            {
                        printf("\n\n~~Main Menu~~");
                        printf("\n==> 1. Insertion and Overflow Demo");
                        printf("\n==> 2. Deletion and Underflow Demo");
                        printf("\n==> 3. Display");
                        printf("\n==> 4. Exit");
                        printf("\nEnter Your Choice: ");
                        scanf("%d", &ch);
                        switch(ch)
                        {
                                    case 1: printf("\n\nEnter the element to be inserted: ");
                                                scanf("%d", &item);
                                                add(item);
                                                break;
                                    case 2: del();
                                                break;
                                   case 3: display();
                                                break;
                                    case 4: exit(0);
                                    default: printf("\n\nPlease enter a valid choice");
                        }
            }
}
void add(char item)
{
            if(front == (rear+1)%MAX)
                        printf("\n\n~~Circular Queue Overflow~~");
            else
            {
                        if(front == -1)
                                    front = rear = 0;
                        else
                                    rear = (rear+1)%MAX;
                        cq[rear] = item;
            }
}

void del()
{
            char item;
            if(front == -1)
            {
                        printf("\n\n~~Circular Queue Underflow~~");
            }
            else
            {
                        item = cq[front];
                        if(front == rear) //only one element
                                    front = rear = -1;
                        else
                                    front = (front+1)%MAX;
                        printf("\n\nDeleted element from the queue is: %d ", item );
            }
}
void display ()
{
            int i ;
            if(front == -1)
            {
                        printf("\n\nCircular Queue Empty");
                        return;
            }
            else
            {
                        printf("\nCircular Queue contents are:\n");
                        printf("\nFront[%d]-> ", front);
                        for(i=front; i!=rear ; i=(i+1)%MAX)
                        {
                                    printf("  %d", cq[i]);
                        }
                        printf("  %d",cq[i]);
                        printf(" <-[%d]Rear", rear);
    }
}

Output:
~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 11

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 22

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 33

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 44
~~Circular Queue Overflow~~

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 3
Circular Queue contents are:
Front[0]->   11  22  33 <-[2]Rear

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 2
Deleted element from the queue is: 11

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 2
Deleted element from the queue is: 22

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 3
Circular Queue contents are:
Front[2]->   33 <-[2]Rear

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 1
Enter the element to be inserted: 66

~~Main Menu~~
==> 1. Insertion and Overflow Demo
==> 2. Deletion and Underflow Demo
==> 3. Display
==> 4. Exit
Enter Your Choice: 3
Circular Queue contents are:
Front[2]->   33  66 <-[0]Rear

No comments:

Post a Comment