-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderStack.py
More file actions
36 lines (30 loc) · 824 Bytes
/
orderStack.py
File metadata and controls
36 lines (30 loc) · 824 Bytes
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
from queue import LifoQueue
def sortStack(stack):
tempStack = LifoQueue()
while stack.empty() == False:
elementToOrder = stack.get()
compareWithTempStack(elementToOrder, stack, tempStack)
while tempStack.empty() == False:
stack.put(tempStack.get())
def compareWithTempStack(elementToOrder, stack, tempStack):
if tempStack.empty():
tempStack.put(elementToOrder)
else:
tempStackElement = tempStack.get()
if elementToOrder >= tempStackElement:
tempStack.put(tempStackElement)
tempStack.put(elementToOrder)
else:
stack.put(tempStackElement)
compareWithTempStack(elementToOrder, stack, tempStack)
def printStack(stack):
while stack.empty() == False:
print(stack.get())
stack = LifoQueue()
stack.put(5)
stack.put(3)
stack.put(4)
stack.put(1)
stack.put(2)
sortStack(stack)
printStack(stack)