-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcustom_q.py
More file actions
54 lines (46 loc) · 1.26 KB
/
custom_q.py
File metadata and controls
54 lines (46 loc) · 1.26 KB
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
class Node:
def __init__(self, data=None, next_node=None) -> None:
self.data = data
self.next_node = next_node
class Queue:
def __init__(self) -> None:
"""Queue Data Structure"""
self.head = None
self.tail = None
def enqueue(self, data):
"""Insert data into `Queue`
Args:
data (Any): data to be inserted
Example:
>>> q = Queue()
>>> q.enqueue(1)
>>> q.enqueue(2)
>>> q.head.data
1
>>> q.tail.data
2
"""
if self.tail is None and self.head is None:
self.tail = self.head = Node(data, None)
return
self.tail.next_node = Node(data, None)
self.tail = self.tail.next_node
return
def dequeue(self):
"""remove data from `Queue`
Returns:
Any: Queue data
Example:
>>> q = Queue()
>>> q.enqueue(1)
>>> q.enqueue(2)
>>> q.dequeue().data
1
"""
if self.head is None:
return None
removed = self.head
self.head = self.head.next_node
if self.head is None:
self.tail = None
return removed