Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
19 lines (16 sloc)
447 Bytes
This file contains 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, n): | |
self.size = n | |
self.data = [None] * n | |
self.top = -1 | |
def push(self, e): | |
if self.top + 1 == self.size: | |
raise Exception('Stack overflowed') | |
self.top += 1 | |
self.data[self.top] = e | |
def pop(self): | |
if self.top == -1: | |
raise Exception('Stack underflowed') | |
e = self.data[self.top] | |
self.top -= 1 | |
return e |