-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStacksAsQueue.py
79 lines (57 loc) · 1.54 KB
/
StacksAsQueue.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Take two stacks and have them act as a Queue
import unittest
class Item():
def __init__(self, data, next=None):
self.data = data
self.next = next
class Stack():
def __init__(self):
self.head = None
self.count = 0
def pop(self):
self.head = self.head.next
self.count -= 1
def push(self, data):
self.head = Item(data, self.head)
self.count += 1
def peek(self):
return self.head.data
def isEmpty(self):
return self.head == None
def isFull(self):
return self.count >= 5
def __len__(self):
return self.count
def __str__(self):
string = "["
current = self.head
while current != None:
string += str(current.data)
current = current.next
if current != None:
string += ","
return string + "]"
class StacksAsQueue():
def __init__(self):
self.stack1 = Stack()
self.stack2 = Stack()
def add(self, data):
self.stack1.push(data)
while not self.stack1.isEmpty():
self.stack2.push(self.stack1.peek())
self.stack1.pop()
def pop(self):
self.stack2.pop()
self.stack1 = Stack()
def peek(self):
return self.stack2.head.data
#setup for testing
test = StacksAsQueue()
test.add(0)
test.add(1)
test.add(2)
test.add(3)
class Test(unittest.TestCase):
def test_StacksAsQueue(self):
self.assertEqual(test.pop(), None)
unittest.main()