Program to implement queues using arrays


// Program to implement queues using arrays

#include<iostream.h>
#include<conio.h>
#include<process.h>

#define max 20

class queue
{
	private:
		int q[max];
		int front,rear;

	public:
		queue()
		{
			front=rear=-1;
		}
		void insert();
		void delet();
		void display();
};



void queue :: insert()
{
	int item;

	if(rear==max-1)
		cout<<"\nQueue full";
	else
	{
		cout<<"\nEnter item to insert: ";
		cin>>item;
		if(front==-1)
			front++;
		q[++rear]=item;
		cout<<"\nItem inserted";
	}
}



void queue :: delet()
{
	if(front==-1 || front>rear)
		cout<<"\nQueue empty";
	else
	{
		cout<<"\nDeleted element is: "<<q[front];
		front++;
	}
}



void queue :: display()
{
	   if(front==-1 || front>rear)
		cout<<"\nQueue empty";
	   else
	   {
		cout<<"\nElements of Queue are: ";
		for(int i=front;i<=rear;i++)
			cout<<q[i]<<"  ";
	   }
}


void main()
{
	queue iq;
	int c;
	clrscr();

	while(1)
	{
		cout<<"\n\n1.Insert\n2.Delete\n3.Display\n4.exit\n";
		cout<<"\nEnter your choice: ";
		cin>>c;

		switch(c)
		{
			case 1:
					iq.insert();
					break;
			case 2:
					iq.delet();
					break;
			case 3:
					iq.display();
					break;
			case 4:
					exit(0);
		}
	}
}
Chitra
Chitra

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in