-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_using_LL.cpp
More file actions
115 lines (97 loc) · 2.33 KB
/
Queue_using_LL.cpp
File metadata and controls
115 lines (97 loc) · 2.33 KB
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
#include <iostream>
#include <climits>
using namespace std;
class Node {
public:
int value;
Node* next;
Node(int value) {
this->value = value;
next = nullptr;
}
};
class Queue {
private:
Node* first;
Node* last;
int length;
public:
Queue(int value) {
Node* newNode = new Node(value);
first = newNode;
last = newNode;
length = 1;
}
~Queue() {
Node* temp = first;
while (first) {
first = first->next;
delete temp;
temp = first;
}
}
void printQueue() {
if (length == 0) {
cout << "Queue: empty" << endl;
return;
}
Node* temp = first;
cout << "Queue: ";
while (temp) {
cout << temp->value;
temp = temp->next;
if (temp) {
cout << " -> ";
}
}
cout << endl;
}
Node* getFirst() {
return first;
}
Node* getLast() {
return last;
}
int getLength() {
return length;
}
void makeEmpty() {
Node* temp;
while (first) {
temp = first;
first = first->next;
delete temp;
}
length = 0;
}
bool isEmpty() {
if (length == 0) return true;
return false;
}
void enqueue(int value) {
Node* newNode = new Node(value);
if (length == 0) {
first = newNode;
last = newNode;
} else {
last->next = newNode;
last = newNode;
}
length++;
}
int dequeue(){
if(length== 0)return INT_MIN;
Node* temp = first;
int ret = first->value;
if(length == 1){
first = nullptr;
last = nullptr;
}
else{
first = first->next;
}
delete temp;
length--;
return ret;
}
};