-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.py
More file actions
50 lines (41 loc) · 1.05 KB
/
Stack.py
File metadata and controls
50 lines (41 loc) · 1.05 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
class Node:
def __init__(self,value):
self.value = value
self.next = None
class Stack:
def __init__(self,value):
new_node = Node(value)
self.bottom = new_node
self.top = new_node
self.height = 1
def print_stack(self):
temp = self.top
while temp is not None:
print(temp.value)
temp = temp.next
def push(self, value):
new_node = Node(value)
if self.height == 0:
self.top = new_node
else:
new_node.next = self.top
self.top = new_node
self.height += 1
def pop(self):
if self.height == 0:
return None
temp = self.top
self.top = self.top.next
temp.next = None
self.height -= 1
return temp
mainStack = Stack(4)
mainStack.push(3)
mainStack.push(2)
mainStack.push(1)
print('Stack before pop():')
mainStack.print_stack()
print('\nPopped node:')
print(mainStack.pop().value)
print('\nStack after pop():')
mainStack.print_stack()