forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ordered_stack.py
33 lines (26 loc) · 1006 Bytes
/
ordered_stack.py
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
#The stack remains always ordered such that the highest value is at the top and the lowest at the bottom
class OrderedStack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push_t(self, item):
self.items.append(item)
def push(self, item): #push method to maintain order when pushing new elements
temp_stack = OrderedStack()
if self.is_empty() or item > self.peek():
self.push_t(item)
else:
while item < self.peek() and not self.is_empty():
temp_stack.push_t(self.pop())
self.push_t(item)
while not temp_stack.is_empty():
self.push_t(temp_stack.pop())
def pop(self):
if self.is_empty():
raise IndexError("Stack is empty")
return self.items.pop()
def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)