Created
August 3, 2015 05:31
-
-
Save zainulhasan/27f776752c223d3596d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************** | |
PriorityQueue.cpp | |
Auther:Syed Zain Ul hasan | |
*****************************/ | |
#include <iostream> | |
#include <stdlib.h> | |
using namespace std; | |
struct Node | |
{ | |
int priority; | |
int data; | |
Node *next; | |
}; | |
class Priority_Queue | |
{ | |
private: | |
Node *front; | |
public: | |
Priority_Queue() | |
{ | |
front = NULL; | |
} | |
void insert(int item, int priority) | |
{ | |
Node *tmp, *q; | |
tmp = new Node; | |
tmp->data = item; | |
tmp->priority = priority; | |
if (front == NULL || priority < front->priority) | |
{ | |
tmp->next = front; | |
front = tmp; | |
} | |
else | |
{ | |
q = front; | |
while (q->next != NULL && q->next->priority <= priority) | |
q=q->next; | |
tmp->next = q->next; | |
q->next = tmp; | |
} | |
} | |
void del() | |
{ | |
Node *tmp; | |
if(front == NULL) | |
cout<<"Queue Underflow\n"; | |
else | |
{ | |
tmp = front; | |
cout<<"Deleted item is: "<<tmp->data<<endl; | |
front = front->next; | |
free(tmp); | |
} | |
} | |
void display() | |
{ | |
Node *ptr; | |
ptr = front; | |
if (front == NULL) | |
cout<<"Queue is empty\n"; | |
else | |
{ cout<<"Queue is :\n"; | |
cout<<"Priority Item\n"; | |
while(ptr != NULL) | |
{ | |
cout<<ptr->priority<<" "<<ptr->data<<endl; | |
ptr = ptr->next; | |
} | |
} | |
} | |
}; | |
int main() | |
{ | |
int choice, item, priority; | |
Priority_Queue pq; | |
do | |
{ | |
cout<<"1.Insert\n"; | |
cout<<"2.Delete\n"; | |
cout<<"3.Display\n"; | |
cout<<"4.Quit\n"; | |
cout<<"Enter your choice : "; | |
cin>>choice; | |
switch(choice) | |
{ | |
case 1: | |
cout<<"Input the item value to be added in the queue : "; | |
cin>>item; | |
cout<<"Enter its priority : "; | |
cin>>priority; | |
pq.insert(item, priority); | |
break; | |
case 2: | |
pq.del(); | |
break; | |
case 3: | |
pq.display(); | |
break; | |
case 4: | |
break; | |
default : | |
cout<<"Wrong choice\n"; | |
} | |
} | |
while(choice != 4); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment