Program to implement queues using lists


// Program to implement queues using lists

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

#define null 0

class queue
{
	private:
		struct node
		{
			int data;
			node *next;
		};
		node *front,*rear;

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

void queue :: insert()
{
	node *x=new node;

	cout<<"\nEnter item to insert: ";
	cin>>x->data;
	x->next=null;

	if(front==null)
		front=x;
	else
		rear->next=x;
		rear=x;
		cout<<"\nItem inserted";

}

void queue :: delet()
{
	node *x;

	if(front==null)
		cout<<"\nQueue empty";
	else
	{
		x=front;
		cout<<"\nDeleted element is: "<<front->data;
		front=front->next;
		delete(x);
	}
}

void queue :: display()
{
       if(front==null)
		cout<<"\nQueue empty";
       else
       {
		cout<<"\nElements of Queue are: ";
		for(node *x=front;x!=null;x=x->next)
			cout<<x->data<<"->";
		cout<<"null";
       }
}


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