C++ program to implement ascending priority queue using linked list.
Ascending priority queue : It is a collection of item in which the items can be inserted arbitrarily but only the smallest element can be removed.
C++ program to implement ascending priority queue using linked list
#include<iostream>
using namespace std;
struct links
{
int data;
links* next;
};
class priority_queue
{
links *front,*rear;
public:
priority_queue()
{
front=NULL,rear=NULL;
}
void insert()
{
links *ne;
int item;
ne=new links;
if(ne!=NULL)
{
cout<<"Enter the element to be inserted ";
cin>>item;
if(front==NULL)
{
rear=front=ne;
rear->next=NULL;
front->data=item;
}
else
{
links *pre=NULL,*temp=front;
while(1)
{
if(temp==NULL)
{
temp=new links;
rear->next=temp;
rear=temp;
rear->data=item;
rear->next=NULL;
break;
}
else if(temp->data>item&&temp==front)
{
temp=new links;
temp->next=front;
temp->data=item;
front=temp;
break;
}
else if(temp->data>item)
{
temp=new links;
temp->next=pre->next;
temp->data=item;
pre->next=temp;
break;
}
pre=temp;
temp=temp->next;
}
}
}
else
cout<<"Overflow";
}
void delet()
{
links *temp;
if(front==NULL)
cout<<"Empty"<<endl;
else
{
cout<<"Deleted element is "<<front->data<<endl;
temp=front;
front=front->next;
delete temp;
}
}
void display()
{
links *temp=front;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
};
int main()
{
int ch,flag=1;
priority_queue queue;
while(flag)
{
cout<<"Enter your choice\n1.)Insert\n2.)Delete\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
queue.insert();
break;
case 2:
queue.delet();
break;
case 3:
queue.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
using namespace std;
struct links
{
int data;
links* next;
};
class priority_queue
{
links *front,*rear;
public:
priority_queue()
{
front=NULL,rear=NULL;
}
void insert()
{
links *ne;
int item;
ne=new links;
if(ne!=NULL)
{
cout<<"Enter the element to be inserted ";
cin>>item;
if(front==NULL)
{
rear=front=ne;
rear->next=NULL;
front->data=item;
}
else
{
links *pre=NULL,*temp=front;
while(1)
{
if(temp==NULL)
{
temp=new links;
rear->next=temp;
rear=temp;
rear->data=item;
rear->next=NULL;
break;
}
else if(temp->data>item&&temp==front)
{
temp=new links;
temp->next=front;
temp->data=item;
front=temp;
break;
}
else if(temp->data>item)
{
temp=new links;
temp->next=pre->next;
temp->data=item;
pre->next=temp;
break;
}
pre=temp;
temp=temp->next;
}
}
}
else
cout<<"Overflow";
}
void delet()
{
links *temp;
if(front==NULL)
cout<<"Empty"<<endl;
else
{
cout<<"Deleted element is "<<front->data<<endl;
temp=front;
front=front->next;
delete temp;
}
}
void display()
{
links *temp=front;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
};
int main()
{
int ch,flag=1;
priority_queue queue;
while(flag)
{
cout<<"Enter your choice\n1.)Insert\n2.)Delete\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
queue.insert();
break;
case 2:
queue.delet();
break;
case 3:
queue.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
No comments:
Post a Comment