Skip to content

Commit d7946b4

Browse files
authored
Create stack.py
1 parent f7da1e7 commit d7946b4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/chapter04stacks/stack.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) Oct 22, 2018 CareerMonk Publications and others.
2+
# E-Mail : info@careermonk.com
3+
# Creation Date : 2014-01-10 06:15:46
4+
# Last modification : 2008-10-31
5+
# by : Narasimha Karumanchi
6+
# Book Title : Data Structures And Algorithmic Thinking With Python
7+
# Warranty : This software is provided "as is" without any
8+
# warranty; without even the implied warranty of
9+
# merchantability or fitness for a particular purpose.
10+
11+
import random
12+
13+
class Stack:
14+
def __init__(self, C = 5):
15+
self.C = C
16+
self.array = []
17+
def size(self):
18+
return len(self.array)
19+
def isEmpty(self):
20+
return len(self.array) == 0
21+
def isFull(self):
22+
return len(self.array) == self.C
23+
def peek(self):
24+
if self.isEmpty():
25+
return None
26+
return self.array[self.size()-1]
27+
def pop(self):
28+
if self.isEmpty():
29+
print "Underflow"
30+
return None
31+
data = self.array.pop()
32+
return data
33+
def push(self, data):
34+
if self.isFull():
35+
print "Overflow"
36+
return
37+
self.array.append(data)
38+
39+
def testStack(stackCap):
40+
stack = Stack(stackCap)
41+
c = stackCap * 2
42+
while(c):
43+
data = random.randrange(1,100)
44+
print "Pushing ", data
45+
stack.push(data)
46+
c -= 1
47+
print "Stack size ", stack.size()
48+
c = stackCap * 2
49+
while(c):
50+
print "Popping", stack.pop()
51+
c -= 1
52+
53+
print testStack(6)

0 commit comments

Comments
 (0)