-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStack.py
More file actions
89 lines (69 loc) · 1.88 KB
/
Copy pathLinkedStack.py
File metadata and controls
89 lines (69 loc) · 1.88 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Stack()
@description: creates a new empty stack, it needs no parameters
@params: no parameters needed
@return: an empty stack
push(item)
@description: appends item to the top of the stack, the stack is modified
@params: the item
@return: nothing
pop()
@description: removes the top item from the stack, the stack is modified
@params: no parameters needed
@return: the item
peek() / top()
@description: gets the top item from the stack
@params: no parameters needed
@return: the top item from the stack
size()
@description: gets the number of items on the stack
@params: no parameters needed
@return: the size of the stack
isEmpty()
@description: tests to see whether the stack is empty
@params: no parameters needed
@return: boolean
"""
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedStack:
def __init__(self):
self.head = None
self.size = 0
def isEmpty(self):
if self.head == None:
return True
return False
def size(self):
return self.size
def push(self, data):
if self.head == None:
self.head = Node(data)
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def pop(self):
if self.isEmpty():
raise Exception("Popping from an empty stack")
popped_node = self.head
self.head = self.head.next
popped_node.next = None
return popped_node.data
def peek(self):
if self.isEmpty():
raise Exception("Peeking from an empty stack")
return self.head.data
def testLinkedStack():
s = LinkedStack()
assert s.isEmpty() == True
s.push(4)
s.push(5)
assert s.isEmpty() == False
assert s.peek() == 5
s.pop()
assert s.peek() == 4
if __name__ == "__main__":
testLinkedStack()
print('ALL TEST CASES PASSED')