Sunday 27 January 2013

Write a program to implement Insertion, deletion and traversing operation in a Circular Queue


#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *ptr,*front=NULL,*rear=NULL,*temp;
int num;
char ch;
clrscr();
cout<<"Inserting elements in the Queue:"<<endl;
do
{
temp=new node;
cout<<"Enter the element to be inserted: ";
cin>>num;
temp->data=num;
if(front==NULL)
{
front=temp;
rear=temp;
temp->link=front;
}
else
{
rear->link=temp;
rear=temp;
temp->link=front;
}
cout<<"do you want to continue inserton(y/n):- ";
cin>>ch;
}while(ch=='y');
cout<<"Elements of queue are:"<<endl;
cout<<front->data<<"\t";
for(ptr=front->link;ptr!=front;ptr=ptr->link)
{
cout<<ptr->data<<"\t";
}
//DELETION
temp=front;
cout<<endl<<"Do you want to perform deletion(y/n) ";
cin>>ch;
if(ch=='y')
{
do
{
if(front==NULL)
{
cout<<"!!Queue is empty!!";
break;
}
else if(front==rear)
{
cout<<"Deleted element is: "<<front->data;
front=NULL;
rear=NULL;
}
else
{
cout<<"Deleted element is: "<<front->data;
front=front->link;
}
cout<<endl<<"Do you want to continue deletion(y/n):- ";
cin>>ch;
}while(ch=='y');
if(front!=NULL)
{
for(ptr=front;ptr!=temp;ptr=ptr->link)
{
cout<<ptr->data<<"\t";
}
}
}
else
cout<<"!!Press any key to exit!!";
getch();
}

No comments:

Post a Comment