-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathusing-queue.py
39 lines (30 loc) · 930 Bytes
/
using-queue.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
# Add each character of a string to a queue, pop characters off queue into a list, and print joined list
class Node:
def __init__(self, value):
self.value = value
self.next_node = None
class Queue:
def __init__(self):
self.front = None
def push(self, value):
node = Node(value)
if self.front is not None:
temp = self.front
while temp.next_node is not None:
temp = temp.next_node
temp.next_node = node
else:
self.front = node
def pop(self):
if self.front is not None:
node_to_pop = self.front
self.front = node_to_pop.next_node
return node_to_pop.value
raise IndexError("Queue Empty")
queue = Queue()
for i in "Hello, world!":
queue.push(i)
result = []
while queue.front is not None:
result.append(queue.pop())
print("".join(result))