-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_priority_queue.cpp
More file actions
45 lines (39 loc) · 968 Bytes
/
linear_priority_queue.cpp
File metadata and controls
45 lines (39 loc) · 968 Bytes
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
#include <bits/stdc++.h>
using namespace std;
struct Node{
int data, priority;
Node *next;
Node(int val) : data(val), priority(0), next(NULL) {}
Node(int val, int p) : data(val), priority(p), next(NULL) {}
};
struct PriorityQueue{
Node *first = NULL, *last = NULL;
void insert(int val, int per){
Node *newNode = new Node(val);
if(first == NULL || per < first->priority){
newNode->next = first;
first = newNode;
}
else{
Node *cur = first;
while(cur != NULL && cur->next->priority <= per){
cur = cur->next;
}
newNode->next = cur->next;
cur = newNode;
}
}
void del(){
if(first == NULL){
cout << "UNDERFLOW.. Queue is empty\n";
}
else{
Node *tmp = first;
first = first->next;
delete tmp;
}
}
};
int main()
{
}