-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedStack.py
56 lines (43 loc) · 1007 Bytes
/
LinkedStack.py
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
class StackNode:
def __init__(self, data):
self.data = data
self.next = None
class LinkedStack:
def __init__(self):
self.root = None
def lengthLS(self):
ref = self.root
lenght = 0
while(ref):
lenght += 1
ref = ref.next
return lenght
def isEmpty(self):
if self.root is None: return True
else: return False
def push(self, data):
new_node = StackNode(data)
new_node.next = self.root
self.root = new_node
def pop(self):
if (self.isEmpty()): return
ref = self.root
self.root = self.root.next
ref = None
def peek(self):
if self.isEmpty(): return
return self.root.data
def printStack(self):
ref = self.root
while(ref):
print(ref.data)
ref = ref.next
if __name__ == '__main__':
s1 = LinkedStack()
s1.push(3)
s1.push(9)
s1.push(27)
s1.pop()
s1.peek()
print("Linked Stack: ")
s1.printStack()