Program to implement deques using lists


// Program to implement deques using lists

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

#define null 0

class deque
{
	struct node
	{
		int data;
		struct node *left,*right;
	};
	node *front,*rear;

	public:

		deque()
		{
			front=rear=null;
		}
		
		void display();
		void insert();
		void delet();
};

void deque :: display()
{
	int ch;
	node *x;

	if(front==null && rear==null)
		cout<<"\nDeque is empty ";
	else
	{
		cout<<"\n\t1.Front End\n\t2.Rear end";
		cout<<"\nEnter your choice : ";
		cin>>ch;

		if(ch==1)
		{
			cout<<"\nElements of deque from front end are : ";
			for(x=front;x!=NULL;x=x->right)
				cout<<x->data<<"<->";
			cout<<"NULL";
		}
		else
		{
			cout<<"\nElements of deque from rear end are : ";
			for(x=rear;x!=null;x=x->left)
				cout<<x->data<<"<->";
			cout<<"NULL";
		}
	}
}


void deque :: insert(void)
{
	node *x;
	int ch;

	x=new node;

	cout<<"\nEnter data to insert : ";
	cin>>x->data;
	x->left=NULL;
	x->right=NULL;

	if(front==null && rear==null)
	{
		front=x;
		rear=x;
	}
	else
	{
		cout<<"\n\t1.Front end\n\t2.Rear end";
		cout<<"\nEnter your choice : ";
		cin>>ch;

		switch(ch)
		{
			case 1:
				x->right=front;
				front->left=x;
				front=x;
				break;

			case 2 :
				x->left=rear;
				rear->right=x;
				rear=x;
				break;
		}
	}
	cout<<"\nElement inserted";
}


void deque :: delet(void)
{
	node *x;
	int ch;

	if(front==null && rear==null)
		cout<<"\nDeque is empty ";
	else
	{
		cout<<"\n\t1.Front End\n\t2.Rear end";
		cout<<"\nEnter your choice : ";
		cin>>ch;

		switch(ch)
		{
			case 1:
				x=front;
				front=front->right;
				if(front==null)
					rear=null;
				else
					front->left=NULL;
				cout<<"\nDeleted element is : "<<x->data;
				break;

			case 2:
				x=rear;
				rear=rear->left;
				if(rear==null)
					front=null;
				else
					rear->right=null;
				cout<<"\nDeleted element is : "<<x->data;
				break;
		}
		delete(x);
	}
}


void main()
{
	deque 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