-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.py
More file actions
66 lines (47 loc) · 1.67 KB
/
Stack.py
File metadata and controls
66 lines (47 loc) · 1.67 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
"""
Author: Kevin Waters
Date: 1 June 2020
Description: This file creates an abstract data type named Stack. Items are pushed to the stack(list) from
index 0 and popped from the stack at index 0 creating a Last In First Out data structure. There is a built in
method to view the next item in th queue as well as methods that return the length of the queue
and if the queue is empty.
"""
import random
class Stack:
def __init__(self):
self.__items = []
def push(self, x):
return self.__items.append(x)
def pop(self):
return self.__items.pop()
def peek(self):
if self.isEmpty():
return None
else:
return self.__items[len(self.__items) - 1]
def len(self):
return len(self.__items)
def isEmpty(self):
return len(self.__items) == 0
def main():
myStack = Stack()
print("The stack is empty == ", myStack.isEmpty())
print("The size of the stack is: ", myStack.len())
print("The item at the top is: ", myStack.peek(), "\n")
for i in range(20):
x = random.randint(-100, 100)
print(x)
myStack.push(x)
print("Hello")
print("The stack is empty == ", myStack.isEmpty())
print("The size of the stack is: ", myStack.len())
print("The item at the top is: ", myStack.peek(), "\n")
while not myStack.isEmpty():
x = myStack.pop()
print("Popped the value", x, "from stack.")
print()
print("The stack is empty == ", myStack.isEmpty())
print("The size of the stack is: ", myStack.len())
print("The item at the top is: ", myStack.peek(), "\n")
if __name__ == "__main__":
main()