-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_DS.py
More file actions
70 lines (60 loc) · 1.9 KB
/
stack_DS.py
File metadata and controls
70 lines (60 loc) · 1.9 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
#Data Structure: Stack
#Stack implementation using a linked list
#
#isEmpty() --> Returns whether the stack is empty --> O(1)
#getSize() --> Returns size of stack --> O(1)
#top() / peek() --> Returns reference to topmost element of stack --> O(1)
#push(a) --> Inserts element 'a' at the top of the stack --> O(1)
#pop() --> Deletes the topmost element of the stack --> O(1)
#
class Node:
def __init__(self, value):
self.value = value
self.next = None
class Stack:
#Stack initialization, using dummy node
def __init__(self):
self.head = Node("head")
self.size = 0
#String representation of stack
def __str__(self):
cur = self.head.next
out = ""
while cur:
out += str(cur.value) + "->"
cur = cur.next
return out[:-2]
#Gets current size of stack
def getSize(self):
return self.size
#Checks if stack is empty returns boolean
def isEmpty(self):
return self.size == 0
#Get top item from stack
def peek(self):
if self.isEmpty():
raise Exception("This is an empty stack")
return self.head.next.value
#Put value into stack
def push(self, value):
node = Node(value)
node.next = self.head.next
self.head.next = node
self.size += 1
#Remove value from stack and return
def pop(self):
if self.isEmpty():
raise Exception("This is an empty stack")
remove = self.head.next
self.head.next = self.head.next.next
self.size -= 1
return remove.value
if __name__ == "__main__":
stack = Stack()
for i in range(1, 11):
stack.push(i)
print(f"Stack: {stack}")
for _ in range(1, 6):
remove = stack.pop()
print(f"Pop: {remove}")
print(f"Stack: {stack}")