-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack_linkedlist.py
More file actions
58 lines (53 loc) · 1.44 KB
/
stack_linkedlist.py
File metadata and controls
58 lines (53 loc) · 1.44 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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.head = None
def push(self, data):
if self.head is None:
new_node = Node(data)
self.head = new_node
else:
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def pop(self):
if self.head is None:
return None
else:
popped = self.head.data
self.head = self.head.next
return popped
def display(self):
if self. head is None:
print("Stack empty")
else:
n = self.head
print(n,end=' ')
while n is not None:
print("-->"+str(n.data)+"("+str(n.next)+")",end=' ')
n = n.next
print()
a_stack = Stack()
while True:
print('1. push <value>')
print('2. pop')
print('3.display')
print('4. quit')
do = input("Enter your choice:" )
operation = do
if operation == '1':
num=int(input("Enter the value"))
a_stack.push(num)
elif operation == '2':
popped = a_stack.pop()
if popped is None:
print('Stack is empty.')
else:
print('Popped value: ', int(popped))
elif operation == '3':
print(a_stack.display())
elif operation == '4':
break