forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_using_stack.go
279 lines (247 loc) · 6.33 KB
/
queue_using_stack.go
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
==============================================================
Implementing QUEUE using STACK data structure
==============================================================
queue can be implemented in two ways:
1 -> By making enQueue() operation costly :
This method push the new element at the top of enQueueStack,
so that deQueue operation just pops from enQueueStack.
To put the element at top of enQueueStack, deQueueStack is used.
2 -> By making deQueue() operation costly :
In this method, in en-queue operation, the new element is entered at the top of enQueueStack.
In de-queue operation, all the elements of enQueueStack are moved to deQueueStack and top of deQueueStack is returned.
finally all the elements of deQueueStack are moved to enQueueStack
This program uses second way
*/
package main
import (
"fmt"
)
// LENGTH of every Stack and Queue in this program
const LENGTH int = 10
// STACK user-definded data type to hold the variables for our stack data structure
type STACK struct {
top int
array [LENGTH]int
}
// init(): this will initialize the stack for use
func (stack *STACK) initStack() {
stack.top = -1
}
// isEmpty(): this checks if stack is empty or not
func (stack *STACK) isEmpty() bool {
if stack.top < 0 {
return true
}
return false
}
// isFull(): this checks if stack is full or not
func (stack *STACK) isFull() bool {
if stack.top >= LENGTH-1 {
return true
}
return false
}
// push(): this will push the value to the top of the stack
func (stack *STACK) push(value int) {
if stack.isFull() {
fmt.Println("Stack is Full")
} else {
stack.top++
stack.array[stack.top] = value
}
}
// pop(): this will remove the value from the top of the stack
func (stack *STACK) pop() int {
var data int
if stack.isEmpty() {
fmt.Println("Stack is empty")
} else {
data = stack.array[stack.top]
stack.top--
}
return data
}
// QUEUE user-defined data type will use for implemnting Queue data structure using Stack
type QUEUE struct {
front int
back int
enQueueStack STACK
deQueueStack STACK
}
func (queue *QUEUE) initQueue() {
queue.front = -1
queue.back = -1
queue.enQueueStack.initStack()
queue.deQueueStack.initStack()
}
func (queue *QUEUE) length() {
if queue.isEmpty() {
fmt.Println("The current length of Queue is 0")
} else {
fmt.Printf("The current length of Queue is %d\n", queue.back-queue.front+1)
}
}
// isEmpty(): checks if the both front and back are less than 0 it means Queue is empty
func (queue *QUEUE) isEmpty() bool {
if queue.front < 0 && queue.back < 0 {
return true
}
return false
}
// iFull(): checks if the back of Queue is greater or equals to last index of array,
// if so it will return true otherwise false
func (queue *QUEUE) isFull() bool {
if queue.back >= LENGTH-1 {
return true
}
return false
}
// enQueue(): it first checks if there is space available for new element,
// if available it will just push the value in the enQueueStack
func (queue *QUEUE) enQueue() {
var data int
if queue.isFull() {
fmt.Println("Can't insert value")
fmt.Println("Queue is full")
} else {
if queue.front < 0 {
queue.front++
}
fmt.Print("enter the value: ")
fmt.Scanf("%d", &data)
queue.back++
queue.enQueueStack.push(data)
fmt.Println("value inserted succesfully at the end of the Queue")
}
}
// deQueue(): if the underlying stacks are not empty then it will pop all elemnt from enQueueStack
// and push them to deQueueStack and return the value by poping it from deQueueStack
// and finally it will pop all element from deQueueStack and push them to enQueueStack again
func (queue *QUEUE) deQueue() {
if queue.front < 0 || queue.front > queue.back {
fmt.Println("Can't delete value")
fmt.Println("Queue is empty")
} else {
for i := 0; i <= queue.back-queue.front; i++ {
queue.deQueueStack.push(queue.enQueueStack.pop())
}
fmt.Printf("Succesfully deleted element %d from the front of the Queue\n", queue.deQueueStack.pop())
queue.front++
for i := 0; i <= queue.back-queue.front; i++ {
queue.enQueueStack.push(queue.deQueueStack.pop())
}
if queue.front > queue.back {
queue.initQueue()
}
}
}
// printQueue(): if queue is not empty then it will simply
// print all the elements of enQueueStack
func (queue *QUEUE) printQueue() {
if queue.isEmpty() {
fmt.Println("Queue is empty")
} else {
fmt.Println("Current state of Queue is")
for i := 0; i <= queue.back-queue.front; i++ {
fmt.Printf("%d ", queue.enQueueStack.array[i])
}
fmt.Print("\n")
}
}
func main() {
var queue QUEUE
queue.initQueue() // initalizing queue for first time
var choice int
var isContinue bool = true
fmt.Println("Please enter option")
for isContinue {
fmt.Println("1 -> insert element\n2 -> delete element\n3 -> print Queue\n4 -> print length\n5 -> exit program")
fmt.Scanf("%d", &choice)
switch choice {
case 1:
queue.enQueue()
break
case 2:
queue.deQueue()
break
case 3:
queue.printQueue()
break
case 4:
queue.length()
break
case 5:
isContinue = false
break
default:
fmt.Println("Please enter correct option")
break
}
}
}
/*
input/output sample
Please enter option
1 -> insert element
2 -> delete element
3 -> print Queue
4 -> print length
5 -> exit program
1
enter the value: 4
value inserted succesfully at the end of the Queue
1 -> insert element
2 -> delete element
3 -> print Queue
4 -> print length
5 -> exit program
1
enter the value: 5
value inserted succesfully at the end of the Queue
1 -> insert element
2 -> delete element
3 -> print Queue
4 -> print length
5 -> exit program
1
enter the value: 6
value inserted succesfully at the end of the Queue
1 -> insert element
2 -> delete element
3 -> print Queue
4 -> print length
5 -> exit program
3
Current state of Queue is
4 5 6
1 -> insert element
2 -> delete element
3 -> print Queue
4 -> print length
5 -> exit program
2
Succesfully deleted element 4 from the front of the Queue
1 -> insert element
2 -> delete element
3 -> print Queue
4 -> print length
5 -> exit program
2
Succesfully deleted element 5 from the front of the Queue
1 -> insert element
2 -> delete element
3 -> print Queue
4 -> print length
5 -> exit program
3
Current state of Queue is
6
1 -> insert element
2 -> delete element
3 -> print Queue
4 -> print length
5 -> exit program
4
The current length of Queue is 1
*/