forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_using_queue.go
288 lines (260 loc) · 6.47 KB
/
stack_using_queue.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
280
281
282
283
284
285
286
287
288
/*
==============================================================
Implementing QUEUE using STACK data structure
==============================================================
stack can be implemented using two queues
1 -> By making push() operation costly :
This method insert new entered element in the pushQueue,
so that pop() operation just dequeue from pushQueue. To put the element at top of pushQueue, popQueue is used.
2 -> By making pop() operation costly :
In this method, in push() operation, the new element is entered at the top of pushQueue.
In pop() operation, all the elements of pushQueue are dequeued and enqueued to popQueue and top of popQueue is returned.
finally all the elements of popQueue dequeued and enqueued to pushQueue again
This program uses second way
*/
package main
import (
"fmt"
)
// CAPACITY of every Stack and Queue in this program
const CAPACITY int = 10
// QUEUE user-definded data type to hold the variables for our queue data structure
type QUEUE struct {
front int
back int
array [CAPACITY]int
}
func (queue *QUEUE) initQueue() {
queue.front = -1
queue.back = -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 >= CAPACITY-1 {
return true
}
return false
}
// enQueue(): it first checks if there is space available for new element,
// if available it will just insert the value to end of the Queue
func (queue *QUEUE) enQueue(value int) {
if queue.isFull() {
fmt.Println("Queue is full")
} else {
if queue.front < 0 {
queue.front++
}
queue.back++
queue.array[queue.back] = value
}
}
// pop(): this will delete the value from the front of the Queue
func (queue *QUEUE) deQueue() int {
var data int
if queue.isEmpty() {
fmt.Println("Queue is empty")
} else {
data = queue.array[queue.front]
queue.front++
if queue.front > queue.back {
queue.initQueue()
}
}
return data
}
// printQueue(): if queue is not empty then it will simply print the element
func (queue *QUEUE) printQueue() {
for i := queue.front; i <= queue.back; i++ {
fmt.Printf("%d ", queue.array[i])
}
}
// STACK user-defined data type will use for implementing stack using queue
type STACK struct {
top int
pushQueue QUEUE
popQueue QUEUE
}
func (stack *STACK) initStack() {
stack.top = -1
stack.pushQueue.initQueue()
stack.popQueue.initQueue()
}
//length(): if the stack is empty it will show 0 element,
// otherwise it will show the number of current element presents in stack
func (stack *STACK) length() {
if stack.isEmpty() {
fmt.Println("The current length of Stack is 0")
} else {
fmt.Printf("The current length of Stack is %d\n", 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 >= CAPACITY-1 {
return true
}
return false
}
// push(): if there is space for new element it will simply insert the element to pushQueue
func (stack *STACK) push() {
var data int
if stack.isFull() {
fmt.Println("Stack is full")
} else {
stack.top++
fmt.Println("enter the value: ")
fmt.Scanf("%d", &data)
stack.pushQueue.enQueue(data)
fmt.Println("value pushed succesfully at the top of the Stack")
}
}
// pop(): if stack is not empty then first it will remove all the element from pushQueue
// and insert them in popQueue and return the poped element by removing popQueue's first element
// and finally it will again insert all the element of popQueue to pushQueue by removing them
func (stack *STACK) pop() {
var data int
if stack.isEmpty() {
fmt.Println("Stack is empty")
} else {
for i := 0; i <= stack.top; i++ {
stack.popQueue.enQueue(stack.pushQueue.deQueue())
}
data = stack.popQueue.deQueue()
stack.top--
for i := 0; i <= stack.top; i++ {
stack.pushQueue.enQueue(stack.popQueue.deQueue())
}
fmt.Printf("Succesfully poped element %d from the top of the Stack\n", data)
}
}
// printStack(): if stack is not empty it will print the elements of pushQueue
func (stack *STACK) printStack() {
if stack.isEmpty() {
fmt.Println("Stack is empty")
} else {
fmt.Println("Current state of Stack is")
stack.pushQueue.printQueue()
fmt.Print("\n")
}
}
func main() {
var stack STACK
stack.initStack() // initalizing stack for first time
var choice int
var isContinue bool = true
fmt.Println("Please enter option")
for isContinue {
fmt.Println("1 -> push element\n2 -> pop element\n3 -> print Stack\n4 -> print length\n5 -> exit program")
fmt.Scanf("%d", &choice)
switch choice {
case 1:
stack.push()
break
case 2:
stack.pop()
break
case 3:
stack.printStack()
break
case 4:
stack.length()
break
case 5:
isContinue = false
break
default:
fmt.Println("Please enter correct option")
break
}
}
}
/*
input/output sample :
Please enter option
1 -> push element
2 -> pop element
3 -> print Stack
4 -> print length
5 -> exit program
1
enter the value:
4
value pushed succesfully at the top of the Stack
1 -> push element
2 -> pop element
3 -> print Stack
4 -> print length
5 -> exit program
1
enter the value:
5
value pushed succesfully at the top of the Stack
1 -> push element
2 -> pop element
3 -> print Stack
4 -> print length
5 -> exit program
1
enter the value:
6
value pushed succesfully at the top of the Stack
1 -> push element
2 -> pop element
3 -> print Stack
4 -> print length
5 -> exit program
3
Current state of Stack is
4 5 6
1 -> push element
2 -> pop element
3 -> print Stack
4 -> print length
5 -> exit program
4
The current length of Stack is 3
1 -> push element
2 -> pop element
3 -> print Stack
4 -> print length
5 -> exit program
2
Succesfully poped element 4 from the top of the Stack
1 -> push element
2 -> pop element
3 -> print Stack
4 -> print length
5 -> exit program
2
Succesfully poped element 5 from the top of the Stack
1 -> push element
2 -> pop element
3 -> print Stack
4 -> print length
5 -> exit program
3
Current state of Stack is
6
1 -> push element
2 -> pop element
3 -> print Stack
4 -> print length
5 -> exit program
4
The current length of Stack is 1
*/