-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacks using array.py
More file actions
56 lines (44 loc) · 1.35 KB
/
Copy pathStacks using array.py
File metadata and controls
56 lines (44 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Stack:
'''
Stack is a linear data structure in which
insert and delete operations are performed
in constant time.
It follows Last In First Out (LIFO)
the element which is inserted last is the
element which exits first.
Time complexity
push-> O(1)
pop-> O(1)
top-> O(1)
isEmpty-> O(1)
'''
def __init__(self, maxsize):
'''creates stack of max capacity maxsize'''
self.__top = -1
self.__arr = [0] * maxsize
self.maxsize = maxsize
def isEmpty(self):
'''returns True if stack is empty'''
return self.__top == -1
def push(self, item):
'''push an item into the stack
example push(item)'''
if self.__top == self.maxsize - 1:
raise Error("Stack Overflow")
self.__top += 1
self.__arr[self.__top] = item
def pop(self):
'''returns the top element and removes it from stack'''
if self.isEmpty():
raise Error("Stack Underflow")
self.__top -= 1
return self.__arr[self.__top + 1]
def top(self):
'''returns the top element without removing'''
if self.__top == -1:
raise Error("Stack Underflow")
return self.__arr[self.__top]
def __len__(self):
return self.__top + 1
class Error(Exception):
pass