-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
57 lines (34 loc) · 1.32 KB
/
stack.py
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
# Stack.py
#
# by David M. Reed and John Zelle
# from Data Structures and Algorithms Using Python and C++
# downloaded from publisher's website:
# https://www.fbeedle.com/content/data-structures-and-algorithms-using-python-and-c
# on July 23, 2014
class Stack(object):
#------------------------------------------------------------
def __init__(self):
'''post: creates an empty LIFO stack'''
self.items = []
#------------------------------------------------------------
def push(self, item):
'''post: places x on top of the stack'''
self.items.append(item)
#------------------------------------------------------------
def pop(self):
'''post: removes and returns the top element of
the stack'''
return self.items.pop()
#------------------------------------------------------------
def top(self):
'''post: returns the top element of the stack without
removing it'''
return self.items[-1]
#------------------------------------------------------------
def size(self):
'''post: returns the number of elements in the stack'''
return len(self.items)
#------------------------------------------------------------
# Added by Tahmid Efaz
def clean(self):
self.items=[]