RSS

IMPLEMENTATION OF CIRCULAR QUEUE USING ARRAYS (2ND LOGIC)

29 Mar
// THIS PROGRAM IMPLEMENTS CIRCULAR QUEUE

#include <iostream>
#include <cstdlib>
#define size 4

using namespace std;

class queue{
	int front,rear;
	int q[size];
	
	public:
		queue()
		{
			front = rear = -1;
		}
		void insert();
		int deleteq();
		void display();
};

int main()
{
	queue q;
	int choice;
	char wish;
	
	do{
		system("cls");
		cout<<"\nIMPLEMENTING CIRCULAR QUEUE USING ARRAYS \n";
		cout<<"\nPLEASE ENTER A CHOICE\n";
		cout<<"\n1. INSERT AN ELEMENT\n";
		cout<<"\n2. DELETE AN ELEMENT\n";
		cout<<"\n3. SHOW QUEUE STATUS\n";
		cout<<"\n4. EXIT FROM THE PROGRAM\n\n\n";
		cin>>choice;
		while(choice <1 || choice > 4)
		{
			cout<<"\n\nInvalid choice\n";
			cout<<"\nPlease enter your choice again\n\n";
			cin>>choice;
		}
		
		int del;
		
			switch(choice)
			{
				case 1: q.insert();
						cout<<"\n\nThe queue after insertion is\n\n";
						q.display();
						break;
				case 2: del = q.deleteq();
						cout<<"\n\n'"<<del<<"'"<<" deleted from the queue\n\n";
						cout<<"\nUpdated queue status is\n\n";
						q.display();
						break;
				case 3: cout<<"\n\nThe queue status is \n\n";
						q.display();
						break;
				case 4: cout<<"\n\nEXITING FROM THE PROGRAM...\n\n";
						exit(0);
			}
			
	cout<<"\n\nDo any other operation?\n\n Press 'y' or 'Y' to continue. \n\nAny other option to exit\n\n";
	cin>>wish;
	}while(wish == 'Y' || wish == 'y');	
	
	cout<<"\nThanks!!! Do come again\n";
	
	return 0;
}

void queue::insert()
{
	int item;
	if((rear+1)%size == front)
	{
		cout<<"\nOverflow !!!!! Terminating.....";
		exit(1);
	}
	else if(front == -1 && rear == -1)
	{
		front = 0;
		rear = 1;
		cout<<"\nEnter the element to be inserted\n";
		cin>>item;
		q[front] = item;
		cout<<"\nInsertion Successful\n\n";
	}
	else
	{
		cout<<"\nEnter the element to be inserted \n";
		cin>>item;
		q[rear] = item;
		rear = (rear+1)%size;
		cout<<"\nInsertion Successful\n\n";
	}
}

int queue::deleteq()
{
	int item;
	if(front == rear)
	{
		cout<<"\nUnderflow!!!. Terminating....\n";
		exit(-1);
	}
	else
	{
		item = q[front];
		front = (front+1)%size;
		cout<<"\nDeletion Successful !!!\n\n";
	}
	
	return item;
}

void queue::display()
{
	int ctr = front;
	
	while(ctr != rear)
	{
		cout<<q[ctr]<<' ';
		ctr = (ctr+1)%size;
	}
}
Advertisements
 
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: