-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathqueue-linked-list-impl.py
51 lines (39 loc) · 1.18 KB
/
queue-linked-list-impl.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
class Node:
def __init__(self, key, next=None):
self.key = key
self.next = next
class Queue:
def __init__(self):
self._head = self._tail = None
self._count: int = 0
def enqueue(self, item):
if self.is_empty():
self._head = self._tail = Node(item)
else:
self._tail.next = Node(item)
self._tail = self._tail.next
self._count += 1
def dequeue(self):
if self.is_empty():
raise Exception("Queue is empty")
ret_value: Node = self._head
if self._head == self._tail:
self._head = self._tail = None
else:
self._head = self._head.next
self._count -= 1
return ret_value.key
def size(self) -> int:
return self._count
def is_empty(self) -> bool:
return self._head is None
def hot_potato(namelist, number):
q: Queue = Queue()
for person in namelist:
q.enqueue(person)
while q.size() > 1:
for i in range(number):
q.enqueue(q.dequeue())
q.dequeue()
return q.dequeue()
print(hot_potato(["Bill", "David", "Susan", "Jane", "Kent", "Brad"], 7))