Created
November 27, 2012 16:36
-
-
Save naimakin/4155307 to your computer and use it in GitHub Desktop.
stack datastructs implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Stack: | |
| def __init__(self): | |
| self.items = [] | |
| def isEmpty(self): | |
| return self.items == [] | |
| def push(self, item): | |
| self.items.append(item) | |
| def pop(self): | |
| return self.items.pop() | |
| def peek(self): | |
| return self.items[len(self.items)-1] | |
| def size(self): | |
| return len(self.items) | |
| import string* | |
| def inftopost(infiexpr): | |
| prec={} | |
| prec["*"]={3} | |
| prec["/"]={3} | |
| prec["+"]={2} | |
| prec["-"]={2} | |
| prec["("]={1} | |
| k=Stack() | |
| plist=[] | |
| tok=infiexpr.split() | |
| for t in tok: | |
| if t in string.uppercase(): | |
| plist.append(t) | |
| if t == "(": | |
| k.pop(t) | |
| if == in ")": | |
| tok = opStack.pop() | |
| while tok != '(': | |
| plist.append(tok) | |
| tok = k.pop() | |
| else: | |
| while(not k.isEmpty()) and (prec[k.peek()]>=prec[t]): | |
| plist.append(k.pop()) | |
| k.push(t) | |
| while not k.isEmpty(): | |
| plist.append(k.pop()) | |
| return string.join(pl) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment