-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_linked_list_oop.py
More file actions
55 lines (45 loc) · 1.17 KB
/
stack_linked_list_oop.py
File metadata and controls
55 lines (45 loc) · 1.17 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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.head = None
def push(self, item): # add to front
new_node = Node(item)
new_node.next = self.head
self.head = new_node
def is_empty(self):
return self.head == None
def pop(self): # remove from front
if self.is_empty():
return None
else:
deleted_node = self.head
self.head = self.head.next
return deleted_node.data
def display(self):
current_node = self.head
print("top")
while current_node != None:
print(current_node.data)
current_node = current_node.next
def peek(self):
if self.is_empty():
return "empty"
else:
return self.head.data
#main
stack1 = Stack()
stack1.push('a')
#print(stack1)
stack1.push('b')
stack1.display()
print("Top: " + stack1.peek())
print("Deleted: " + stack1.pop())
stack1.display()
print()
print("pop 2 more times")
print("----------------------------------------")
print(stack1.pop())
print(stack1.pop())