forked from krishnamanojpvr/DSCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
56_PriorityQueue_LinkedList.cpp
90 lines (86 loc) · 1.75 KB
/
56_PriorityQueue_LinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
using namespace std;
class Node
{
public:
int data;
int priority;
Node *next = NULL;
Node(int data, int priority)
{
this->data = data;
this->priority = priority;
this->next = NULL;
}
};
class Queue
{
Node *front;
public:
Queue(int data, int priority)
{
Node *newnode = new Node(data, priority);
front = newnode;
}
Queue()
{
front = NULL;
}
void insert(int data, int priority)
{
Node *newnode = new Node(data, priority);
if (front == NULL || priority < front->priority)
{
newnode->next = front;
front = newnode;
return;
}
Node *temp = front;
while (temp->next != NULL && temp->next->priority <= priority)
{
temp = temp->next;
}
newnode->next = temp->next;
temp->next = newnode;
}
void del()
{
if (front == NULL)
{
cout << "Queue Underflow" << endl;
return;
}
Node *temp = front;
front = front->next;
delete temp;
}
void Display()
{
if (front == NULL){
cout << "Queue is empty"<<endl;
return;
}
Node *temp = front;
cout << "Queue is :"<<endl;
cout << "Priority , Item\n";
while (temp!= NULL)
{
cout << temp->priority << " , " << temp->data << endl;
temp = temp->next;
}
}
};
int main()
{
Queue q1(10, 1);
q1.Display();
q1.insert(15, 3);
q1.insert(20, 2);
q1.insert(30, 4);
q1.Display();
q1.del();
q1.Display();
q1.del();
q1.Display();
return 0;
}