-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack3.py
More file actions
68 lines (51 loc) · 1.51 KB
/
Copy pathStack3.py
File metadata and controls
68 lines (51 loc) · 1.51 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
class Node (object):
def __init__(self,initdata):
self.data = initdata
self.next = None # always do this – saves a lot
# of headaches later!
def getData (self):
return self.data # returns a POINTER
def getNext (self):
return self.next # returns a POINTER
def setData (self, newData):
self.data = newData # changes a POINTER
def setNext (self,newNext):
self.next = newNext # changes a POINTER
class Stack ():
def __init__(self):
self.head = None
def isEmpty (self):
return self.head == None
def push (self, item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def pop (self):
item = self.head.data
self.head = self.head.getNext()
return item
def peek (self):
return self.head.data
def size (self):
current = self.head
count = 0
while current:
current = current.getNext()
count += 1
return count
##################################################
def main():
print("Running Stack3.py")
myStack = Stack()
print(myStack.isEmpty())
myStack.push("cat")
myStack.push(4)
print(myStack.peek())
myStack.push(False)
print(myStack.size())
print(myStack.isEmpty())
myStack.push(98.6)
print(myStack.pop())
print(myStack.pop())
print(myStack.size())
main()