-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.cpp
183 lines (145 loc) · 3.29 KB
/
PriorityQueue.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// main.cpp
// P&D: Priority Queue
//
// Created by Julia Vister on 03/06/2022.
// Priority queue as a linked list:
#include <iostream>
#include <stdexcept>
#include <queue>
#include <string>
struct Customer{
int priority;
std::string name;
};
struct Node{
Node *next;
Node *prev;
Customer value;
Node(Customer value): value(value),next(nullptr),prev(nullptr) {}
/* Node(Customer _value,Node *_next=nullptr,Node *prev=nullptr):
value(_value),
next(_next)
{} */
};
class Priorityqueue{
Node *head;
Node *tail;
public:
Priorityqueue():
head(nullptr),
tail(nullptr)
{}
Node *begin(){
return head;
}
bool empty(){
return head==nullptr;
}
void insert_before(Node *n, Customer value){
if(n==nullptr){
push(value);
}
if(n==head){
push(value);
}
}
void Insert(Customer value){
Node *new_node =new Node(value);
new_node->value=value;
new_node->next=nullptr;
if(head==nullptr){
head=new_node;
return;
}
else{
new_node->next=head;
head=new_node;
}
}
void push(Customer value){
Node *tmp = new Node(value);
if(head==nullptr){
head=tmp;
tail=head;
return;
}
tmp->next=head;
head=tmp;
while(tmp->next!=nullptr){
if(tmp->value.priority > tmp->next->value.priority){
std::swap(tmp->value, tmp->next->value);
}
else
{
return;
}
tmp=tmp->next;
}
}
void pop(){
if(empty()){
throw std::runtime_error("Cannot remove from empty queue!");
}
Node *tmp=head;
head=head->next;
delete tmp;
}
Customer front(){
if(empty()){
throw std::runtime_error("Cannot front value from empty queue!");
}
return head->value; //returning customer
}
void clear(){
while(!empty()){
pop();
}
}
~Priorityqueue() {
clear();
}
Priorityqueue(const Priorityqueue& original):head(nullptr){
Node *tmp = original.head;
while (tmp != nullptr) {
push(tmp->value);
tmp = tmp -> next;
}
}
Priorityqueue &operator=(Priorityqueue cpy){
std::swap(cpy.head, head);
return *this;
}
void print(){
Node *tmp=head;
while(tmp!=nullptr){
std::cout<< tmp->value.priority << " ";
std::cout<< tmp->value.name << " ";
tmp=tmp->next;
}
std::cout<<std::endl;
}
};
int main() {
Priorityqueue q;
Customer V;
V.priority=10;
V.name="Julia";
Customer H;
H.name="Dorsa";
H.priority=8;
Customer G;
G.name="Piotr";
G.priority=20;
q.push(V);
q.push(H);
q.push(G);
G.name="Piotr2";
G.priority=9;
q.push(G);
q.print();
q.pop();
q.print();
q.pop();
q.print();
return 0;
}