Thursday, July 7, 2016

CIRCULAR QUEUE IN DATA STRUCTURE


A circular queue is also a simply a type of queue in which an element is inserted from the rear and deleted from the front. It is differing from the linear queue by that in this the insertion process is continuously done as the queue is full. If the rear is equal to form and again data can be inserted from the beginning of rear.

Program to perform various operation in linear circular implemented in C++.



#include<iostream>
#define size 5
using namespace std;
class queue
{
    private:
        int front,rear;
        int a[size];
    public:
        queue()
        {
            front=-1;
            rear=-1;
        }
        void ins(int q)
        {
            if ((front == 0 && rear == size-1) || (front == rear +1))
                {
                    cout<<"\nQueue Overflow \n";
                    return;
                }
            else
                if (front == -1)
                    {
                        front = 0;
                        rear = 0;
                    }
                else
                    if (rear == size-1)
                        rear = 0;
                    else
                        rear = rear + 1;
                    a[rear] = q;
                    cout<<a[rear]<<" enqueued\n";
            }
        void remv()
        {
            if (front == -1)
                {
                    cout<<"\nQueue Underflow\n";
                    return;
                }
            cout<<a[front]<<" dequeued\n";
            if (front == rear)
                {
                    front = -1;
                    rear = -1;
                }
            else
                if(front == size-1)
                    front = 0;
                else
                    front = front + 1;
        }
};

int main()
{
    queue qu;
    qu.ins(1);
    qu.ins(2);
    qu.ins(3);
    qu.ins(4);
    qu.ins(5);
cout<<"\n";
    qu.remv();
    qu.remv();
    qu.ins(6);
    qu.remv();
    qu.remv();
    qu.remv();
    return 0;
}

Program to perform various operation in circular queue using pointer implemented in C++.


include<conio.h>
#include<windows.h>
#include<iostream>
#define MAX 50
using namespace std;
class circular_queue
{
    int cqueue_arr[MAX];
    int front,rear;
public:
        circular_queue()
        {
            front = -1;
            rear = -1;
        }

void insert()
{
int added_item;
if ((front == 0 && rear == MAX-1) || (front == rear +1))
    {
    cout<<"\nQueue Overflow \n";
    getch();
    return;
    }
if (front == -1)
    {
    front = 0;
    rear = 0;
    }
else
if (rear == MAX-1)
    rear = 0;
else
    rear = rear + 1;
    cout<<"\nInput the element for insertion in queue:";
    cin>>added_item;
    cqueue_arr[rear] = added_item;
}
void del()
{
if (front == -1)
    {
        cout<<"\nQueue Underflow\n";
        return;
    }
cout<<"\nElement deleted from queue is:"<<cqueue_arr[front]<<"\n";
if (front == rear)
    {
    front = -1;
    rear = -1;
    }
else
    if(front == MAX-1)
        front = 0;
    else
        front = front + 1;
}

void display()
{
int front_pos = front,rear_pos = rear;
if (front == -1)
    {
        cout<<"\nQueue is empty\n";
        return;
    }
cout<<"\nQueue elements:\n";
if(front_pos <= rear_pos )
    while(front_pos <= rear_pos)
    {
        cout<<cqueue_arr[front_pos]<<", ";
        front_pos++;
    }
else
    {
    while(front_pos <= MAX-1)
        {
        cout<<cqueue_arr[front_pos]<<", ";
        front_pos++;
        }
    front_pos = 0;
    while(front_pos <= rear_pos)
        {
            cout<<cqueue_arr[front_pos]<<", ";
            front_pos++;
        }
    }
    cout<<"\n";
}

};

int main()
{
int choice;
circular_queue co;
while(1)
    {
    system("cls");
    cout <<"\n1.Insert\n";
    cout <<"2.Delete\n";
    cout <<"3.Display\n";
    cout <<"4.Quit\n";
    cout <<"\nEnter your choice: ";
    cin>>choice;
    switch(choice)
        {
        case 1:
            co.insert();
            break;
        case 2 :
            co.del();
            getch();
            break;
        case 3:
            co.display();
            getch();
            break;
        case 4:
            exit(1);
        default:
            cout<<"\nWrong choice\n";
            getch();
        }
    }
return 0;
}




No comments:

Post a Comment