Skip to content

Commit 578d988

Browse files
committed
Added stack implementation
1 parent af1a915 commit 578d988

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

pygorithm/data_structures/__init__.py

Whitespace-only changes.

pygorithm/data_structures/stack.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Author: OMKAR PATHAK
2+
# Created On: 3rd August July 2017
3+
4+
# stack implementation
5+
class Stack(object):
6+
'''
7+
size : return the current size of the stack
8+
push : push an item into the stack
9+
pop : pop the topmost item from the stack
10+
peek : return the topmost element of the stack
11+
isEmpty : check if the stack is empty
12+
13+
'''
14+
15+
def __init__(self, limit = 10):
16+
'''
17+
@param : limit: the stack size
18+
'''
19+
self.stack = []
20+
self.limit = limit
21+
22+
# for printing the stack contents
23+
def __str__(self):
24+
return ' '.join([str(i) for i in self.stack])
25+
26+
# for pushing an element on to the stack
27+
def push(self, data):
28+
if len(self.stack) >= self.limit:
29+
return -1 # indicates stack overflow
30+
else:
31+
self.stack.append(data)
32+
33+
# for popping the uppermost element
34+
def pop(self):
35+
if len(self.stack) <= 0:
36+
return -1 # indicates stack underflow
37+
else:
38+
return self.stack.pop()
39+
40+
# for peeking the top-most element of the stack
41+
def peek(self):
42+
if len(self.stack) <= 0:
43+
return -1 # stack underflow
44+
else:
45+
return self.stack[len(self.stack) - 1]
46+
47+
# to check if stack is empty
48+
def isEmpty(self):
49+
return self.stack == []
50+
51+
# for checking the size of stack
52+
def size(self):
53+
return len(self.stack)
54+
55+
# easily retrieve the source code of the Stack class
56+
def get_code(self):
57+
import inspect
58+
return inspect.getsource(Stack)
59+
60+
class InfixToPostfix(object):
61+
'''
62+
infix_to_postfix : get the postfix of the given infix expression
63+
64+
'''
65+
66+
def __init__(self, expression = [], stack = None):
67+
'''
68+
@param: expression : the infix expression to be converted to postfix
69+
@param: stack : stack to perform infix to postfix operation
70+
'''
71+
self.myExp = list(expression)
72+
self.myStack = stack
73+
74+
# function to check whether the given character of the expression is operand
75+
def _isOperand(self, char):
76+
return (ord(char) >= ord('a') and ord(char) <= ord('z')) or (ord(char) >= ord('A') and ord(char) <= ord('Z'))
77+
78+
# self defined precedence for each operator
79+
def _precedence(self, char):
80+
if char == '+' or char == '-':
81+
return 1
82+
elif char == '*' or char == '/':
83+
return 2
84+
elif char == '^':
85+
return 3
86+
else:
87+
return -1
88+
89+
# function to convert infix to postfix
90+
def infix_to_postfix(self):
91+
postFix = []
92+
for i in range(len(self.myExp)):
93+
if (self._isOperand(self.myExp[i])):
94+
postFix.append(self.myExp[i])
95+
elif(self.myExp[i] == '('):
96+
self.myStack.push(self.myExp[i])
97+
elif(self.myExp[i] == ')'):
98+
topOperator = self.myStack.pop()
99+
while(not self.myStack.isEmpty() and topOperator != '('):
100+
postFix.append(topOperator)
101+
topOperator = self.myStack.pop()
102+
else:
103+
while (not self.myStack.isEmpty()) and (self._precedence(self.myExp[i]) <= self._precedence(self.myStack.peek())):
104+
postFix.append(self.myStack.pop())
105+
self.myStack.push(self.myExp[i])
106+
107+
while(not self.myStack.isEmpty()):
108+
postFix.append(self.myStack.pop())
109+
return ' '.join(postFix)
110+
111+
# easily retrieve the source code of the Stack class
112+
def get_code(self):
113+
import inspect
114+
return inspect.getsource(InfixToPostfix)

0 commit comments

Comments
 (0)