-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_LinkedList-Functions.cpp
More file actions
188 lines (177 loc) · 4.21 KB
/
Queue_LinkedList-Functions.cpp
File metadata and controls
188 lines (177 loc) · 4.21 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
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
184
185
186
187
188
#include <iostream>
#include <string>
using namespace std;
struct Node
{
int n;
Node *next;
};
class Queue
{
public:
Node *L;
int len;
int count;
//---------------------------------------//
//-----------------QUEUE-----------------//
//---------------------------------------//
Queue()
{
}
Queue(int n)
{
len = n;
count = 0;
L = NULL;
}
//---------------------------------------//
//----------------IsEMPTY----------------//
//---------------------------------------//
bool isEmpty()
{
if (L == NULL && count == 0)
{
return true;
}
else
{
return false;
}
}
//---------------------------------------//
//----------------IsFULL-----------------//
//---------------------------------------//
bool isFull()
{
if (count == len)
{
return true;
}
else
{
return false;
}
}
//---------------------------------------//
//----------------DISPLAY----------------//
//---------------------------------------//
void display()
{
cout << ">QUEUE->>";
Node *current = L;
while (current != NULL)
{
cout << " [" << current->n << "]";
current = current->next;
}
cout << "\n============================\n";
}
//---------------------------------------//
//----------------ENQUEUE----------------//
//---------------------------------------//
void enQueue(int num)
{
if (isEmpty())
{
Node *current = L;
Node *newNode = new Node;
newNode->n = num;
newNode->next = NULL;
L = newNode;
cout << "-->ENQUEUED..!\n";
count++;
}
////////////////////////////////////
else if (isFull())
{
cout << "--> Cannot be enQueued.\n";
}
////////////////////////////////////
else
{
Node *current = L;
Node *newNode = new Node;
newNode->n = num;
newNode->next = NULL;
while (current->next != NULL)
{
current = current->next;
}
current->next = newNode;
cout << "-->ENQUEUED..!\n";
count++;
}
}
//---------------------------------------//
//----------------DEQUEUE----------------//
//---------------------------------------//
int deQueue()
{
if (isEmpty())
{
cout << "--> Cannot be deQueued.\n ";
return 0;
}
else
{
Node *current = L;
int x = current->n;
if (current != NULL)
{
L = L->next;
delete current;
}
count--;
cout << "--> DEQUEUED>> [";
return x;
}
}
};
int main(int argc, char **argv)
{
int size;
int choice;
cout << "\n============================\n";
cout << " > 0: EXIT.\n";
cout << " > 1: EnQUEUE\n";
cout << " > 2: DeQUEUE.\n";
cout << " > 3: DISPLAY.\n";
cout << "============================\n";
cout << ">>>> ENTER SIZE OF QUEUE: ";
cin >> size;
cout << "============================\n";
Queue q(size);
while (choice != 0)
{
cout << "-->> ENTER CHOICE: ";
cin >> choice;
cout << "============================\n";
if (choice == 0)
{
cout << "--EXITTING--\n";
cout << "============================\n";
}
else if (choice == 1)
{
int val;
cout << "ENTER VALUE:";
cin >> val;
q.enQueue(val);
cout << "============================\n";
}
else if (choice == 2)
{
cout << q.deQueue() << "]";
cout << "\n============================\n";
}
else if (choice == 3)
{
q.display();
}
else
{
cout << "--YOU ENTERED WRONG CHOICE--\n--EXITTING--\n";
choice = 0;
}
}
}