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 4
4
# stack implementation
5
5
class Stack (object ):
6
6
'''
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
19
8
'''
20
9
def __init__ (self , limit = 10 ):
21
10
'''
@@ -28,28 +17,36 @@ def __str__(self):
28
17
return ' ' .join ([str (i ) for i in self .stack ])
29
18
30
19
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
+ '''
32
23
if len (self .stack ) >= self .limit :
33
24
return - 1 # indicates stack overflow
34
25
else :
35
26
self .stack .append (data )
36
27
37
28
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
+ '''
39
32
if len (self .stack ) <= 0 :
40
33
return - 1 # indicates stack underflow
41
34
else :
42
35
return self .stack .pop ()
43
36
44
37
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
+ '''
46
41
if len (self .stack ) <= 0 :
47
42
return - 1 # stack underflow
48
43
else :
49
44
return self .stack [len (self .stack ) - 1 ]
50
45
51
46
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
+ '''
53
50
return self .size () == 0
54
51
55
52
def size (self ):
You can’t perform that action at this time.
0 commit comments