Skip to content

Instantly share code, notes, and snippets.

@0xPratik
Created April 11, 2022 19:10
Show Gist options
  • Save 0xPratik/95543d1d755b6d7acb3e8ae8a3e4f1f0 to your computer and use it in GitHub Desktop.
Save 0xPratik/95543d1d755b6d7acb3e8ae8a3e4f1f0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct n // node declaration
{
int p;
int info;
struct n *l;
};
class Priority_Queue {
private:
n *f;
public:
Priority_Queue() //constructor
{
f = NULL;
}
void insert(int i, int p) {
n *t, *q;
t = new n;
t->info = i;
t->p = p;
if (f == NULL || p < f->p) {
t->l= f;
f = t;
} else {
q = f;
while (q->l != NULL && q->l->p <= p)
q = q->l;
t->l = q->l;
q->l = t;
}
}
void del() {
n *t;
if(f == NULL) //if queue is null
cout<<"Queue Underflow\n";
else {
t = f;
cout<<"Deleted item is: "<<t->info<<endl;
f = f->l;
free(t);
}
}
};
int main() {
int c, i, p;
Priority_Queue pq;
do //perform switch opeartion
{
cout<<"1.Enqueue\n";
cout<<"2.Dequeue\n";
cout<<"3.Exit\n";
cout<<"Enter your choice : ";
cin>>c;
switch(c) {
case 1:
cout<<"Input the item value to be added in the queue : ";
cin>>i;
cout<<"Enter its priority : ";
cin>>p;
pq.insert(i, p);
break;
case 2:
pq.del();
break;
case 3:
break;
default:
cout<<"Wrong choice\n";
}
}
while(c != 3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment