File tree Expand file tree Collapse file tree 1 file changed +13
-16
lines changed
pygorithm/data_structures Expand file tree Collapse file tree 1 file changed +13
-16
lines changed Original file line number Diff line number Diff line change 44# stack implementation
55class Stack (object ):
66 '''
7- 1. `push`: pushes an item on to the stack
8-
9- myStack = Stack()
10- myStack.push(10)
11- print(myStack)
12-
13- 2. `pop`: pops the topmost item of the stack
14-
15- myStack = Stack()
16- myStack.push(10)
17- myStack.push(2)
18- print(myStack.pop())
7+ Python implementation of Stack
198 '''
209 def __init__ (self , limit = 10 ):
2110 '''
@@ -28,28 +17,36 @@ def __str__(self):
2817 return ' ' .join ([str (i ) for i in self .stack ])
2918
3019 def push (self , data ):
31- ''' pushes an item into the stack '''
20+ ''' pushes an item into the stack
21+ returns -1 if the stack is empty
22+ '''
3223 if len (self .stack ) >= self .limit :
3324 return - 1 # indicates stack overflow
3425 else :
3526 self .stack .append (data )
3627
3728 def pop (self ):
38- ''' pops the topmost item from the stack '''
29+ ''' pops the topmost item from the stack
30+ returns -1 if the stack is empty
31+ '''
3932 if len (self .stack ) <= 0 :
4033 return - 1 # indicates stack underflow
4134 else :
4235 return self .stack .pop ()
4336
4437 def peek (self ):
45- ''' returns the topmost element of the stack '''
38+ ''' returns the topmost element of the stack
39+ returns -1 if the stack is empty
40+ '''
4641 if len (self .stack ) <= 0 :
4742 return - 1 # stack underflow
4843 else :
4944 return self .stack [len (self .stack ) - 1 ]
5045
5146 def is_empty (self ):
52- ''' checks if the stack is empty '''
47+ ''' checks if the stack is empty
48+ returns boolean value, True or False
49+ '''
5350 return self .size () == 0
5451
5552 def size (self ):
You can’t perform that action at this time.
0 commit comments