-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
68 lines (48 loc) · 1.34 KB
/
stack.py
File metadata and controls
68 lines (48 loc) · 1.34 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
# LAB10.py
class Node:
def __init__(self, value):
self.value = value
self.next = None
def getValue(self):
return self.value
def getNext(self):
return self.next
def setValue(self, new_value):
self.value = new_value
def setNext(self, new_next):
self.next = new_next
def __str__(self):
return "{}".format(self.value)
__repr__ = __str__
class Stack:
def __init__(self):
self.top = None # Do NOT modify this line
self.count = 0
def isEmpty(self):
if self.count == 0:
return True
else:
return False
def size(self):
return self.count
def push(self, item):
new_item = Node(item)
new_item.setNext(self.top)
self.top = new_item
self.count += 1
def pop(self):
popped_value = self.top.getValue()
next_item = self.top.getNext()
del self.top
self.top = next_item
self.count -= 1
return popped_value
def peek(self):
return self.top.value
def printStack(self):
temp = self.top
while temp:
print(temp.getValue())
temp = temp.getNext()
# Collaboration Statement:
# I worked on the homework(lab) assignment alone, using only previous and current course materials.