-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathreverse_stack.py
More file actions
65 lines (50 loc) · 1.25 KB
/
Copy pathreverse_stack.py
File metadata and controls
65 lines (50 loc) · 1.25 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
class Stack:
# create empty list
def __init__(self):
self.Elements = []
# push() for insert an element
def push(self, value):
self.Elements.append(value)
# pop() for remove an element
def pop(self):
return self.Elements.pop()
# empty() check the stack is empty of not
def empty(self):
return self.Elements == []
# show() display stack
def show(self):
for value in reversed(self.Elements):
print(value)
# Insert_Bottom() insert value at bottom
def BottomInsert(s, value):
# check the stack is empty or not
if s.empty():
# if stack is empty then call
# push() method.
s.push(value)
# if stack is not empty then execute
# else block
else:
popped = s.pop()
BottomInsert(s, value)
s.push(popped)
# Reverse() reverse the stack
def Reverse(s):
if s.empty():
pass
else:
popped = s.pop()
Reverse(s)
BottomInsert(s, popped)
# create object of stack class
stk = Stack()
stk.push(1)
stk.push(2)
stk.push(3)
stk.push(4)
stk.push(5)
print("Original Stack")
stk.show()
print("\nStack after Reversing")
Reverse(stk)
stk.show()