-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked-list-queue.cpp
143 lines (122 loc) · 2.98 KB
/
Linked-list-queue.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
// main.cpp
// P&D 9. Linked list Queue
//
// Created by Julia Vister on 25/05/2022.
#include <iostream>
#include <stdexcept>
struct Node {
int value;
Node *next;
Node(int _value,Node *_next=nullptr):
value(_value),
next(_next)
{}
};
class Queue{
Node *head;
Node *tail;
public:
Queue():
head(nullptr),
tail(nullptr)
{}
bool empty()const {
return (head==nullptr);
}
void push(int x){ //inserting values
if(empty()){
head=new Node(x);
tail=head;
}
else{
tail->next=new Node(x);
tail=tail->next;
}
}
int front(){ //showing value
if(empty()){
throw std::runtime_error("Cannot front value from empty queue!");
}
return head->value;
}
void pop(){ //removing values
if(empty()){
throw std::runtime_error("Cannot remove from empty queue!");
}
Node *tmp=head;
head=head->next;
delete tmp;
}
void clear(){
while(!empty()){
Node *tmp=head;
head=head->next;
delete tmp;
}
}
~Queue() { //destructor
clear();
}
Queue(const Queue& original):head(nullptr){ //copy constructor
if(!original.empty()){
head=new Node(original.head->value);
Node *source =original.head->next;
Node *destination=head;
while(source!=nullptr){
destination->next=new Node(source->value);
destination=destination->next;
source=source->next;
}
tail=destination;
}
}
void extend(const Queue& original){ //copies from another queue
Node *tmp=original.head;
while(tmp !=nullptr){
push(tmp->value);
tmp=tmp->next;
}
}
Queue &operator=(Queue cpy){ //assignment operator
std::swap(cpy.head, head);
std::swap(cpy.tail, tail);
return *this;
}
Queue merge(Queue &original, Queue &other){ //produces a third queue
Queue Result;
}
while(!orginal.empty() && !other.empty()){
if(original.push() < other.push()){
original.pop();
result.push(original.front());
}
else if(other.push()> original.push())
{
other.pop();
result.push(other.push);
}
return Result;
}
};
int main() {
Queue q;
q.push(5);
std::cout<< q.front() <<std::endl;
q.pop();
q.push(10);
std::cout<< q.front() <<std::endl;
q.pop();
q.push(15);
std::cout<< q.front() <<std::endl;
q.pop();
q.push(20);
std::cout<< q.front() <<std::endl;
puts("");
Queue w;
w.push(25);
w.push(30);
w.push(35);
w.push(40);
std::cout << "Successfull linked list Queue!" <<std::endl;
return 0;
}