Skip to content

Commit 7376add

Browse files
mariuszskoncclauss
authored andcommitted
Implement Linked Queue and Linked Stack data structures (TheAlgorithms#1324)
* Add LinkedQueue * Add LinkedStack
1 parent e177198 commit 7376add

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

data_structures/queue/linked_queue.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
""" A Queue using a Linked List like structure """
2+
from typing import Any, Optional
3+
4+
5+
class Node:
6+
def __init__(self, data: Any, next: Optional["Node"] = None):
7+
self.data: Any = data
8+
self.next: Optional["Node"] = next
9+
10+
11+
class LinkedQueue:
12+
"""
13+
Linked List Queue implementing put (to end of queue),
14+
get (from front of queue) and is_empty
15+
16+
>>> queue = LinkedQueue()
17+
>>> queue.is_empty()
18+
True
19+
>>> queue.put(5)
20+
>>> queue.put(9)
21+
>>> queue.put('python')
22+
>>> queue.is_empty();
23+
False
24+
>>> queue.get()
25+
5
26+
>>> queue.put('algorithms')
27+
>>> queue.get()
28+
9
29+
>>> queue.get()
30+
'python'
31+
>>> queue.get()
32+
'algorithms'
33+
>>> queue.is_empty()
34+
True
35+
>>> queue.get()
36+
Traceback (most recent call last):
37+
...
38+
IndexError: get from empty queue
39+
"""
40+
41+
def __init__(self) -> None:
42+
self.front: Optional[Node] = None
43+
self.rear: Optional[Node] = None
44+
45+
def is_empty(self) -> bool:
46+
""" returns boolean describing if queue is empty """
47+
return self.front is None
48+
49+
def put(self, item: Any) -> None:
50+
""" append item to rear of queue """
51+
node: Node = Node(item)
52+
if self.is_empty():
53+
# the queue contains just the single element
54+
self.front = node
55+
self.rear = node
56+
else:
57+
# not empty, so we add it to the rear of the queue
58+
assert isinstance(self.rear, Node)
59+
self.rear.next = node
60+
self.rear = node
61+
62+
def get(self) -> Any:
63+
""" returns and removes item at front of queue """
64+
if self.is_empty():
65+
raise IndexError("get from empty queue")
66+
else:
67+
# "remove" element by having front point to the next one
68+
assert isinstance(self.front, Node)
69+
node: Node = self.front
70+
self.front = node.next
71+
if self.front is None:
72+
self.rear = None
73+
74+
return node.data
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
""" A Stack using a Linked List like structure """
2+
from typing import Any, Optional
3+
4+
5+
class Node:
6+
def __init__(self, data: Any, next: Optional["Node"] = None):
7+
self.data: Any = data
8+
self.next: Optional["Node"] = next
9+
10+
11+
class LinkedStack:
12+
"""
13+
Linked List Stack implementing push (to top),
14+
pop (from top) and is_empty
15+
16+
>>> stack = LinkedStack()
17+
>>> stack.is_empty()
18+
True
19+
>>> stack.push(5)
20+
>>> stack.push(9)
21+
>>> stack.push('python')
22+
>>> stack.is_empty();
23+
False
24+
>>> stack.pop()
25+
'python'
26+
>>> stack.push('algorithms')
27+
>>> stack.pop()
28+
'algorithms'
29+
>>> stack.pop()
30+
9
31+
>>> stack.pop()
32+
5
33+
>>> stack.is_empty()
34+
True
35+
>>> stack.pop()
36+
Traceback (most recent call last):
37+
...
38+
IndexError: pop from empty stack
39+
"""
40+
41+
def __init__(self) -> None:
42+
self.top: Optional[Node] = None
43+
44+
def is_empty(self) -> bool:
45+
""" returns boolean describing if stack is empty """
46+
return self.top is None
47+
48+
def push(self, item: Any) -> None:
49+
""" append item to top of stack """
50+
node: Node = Node(item)
51+
if self.is_empty():
52+
self.top = node
53+
else:
54+
# each node points to the item "lower" in the stack
55+
node.next = self.top
56+
self.top = node
57+
58+
def pop(self) -> Any:
59+
""" returns and removes item at top of stack """
60+
if self.is_empty():
61+
raise IndexError("pop from empty stack")
62+
else:
63+
# "remove" element by having top point to the next one
64+
assert isinstance(self.top, Node)
65+
node: Node = self.top
66+
self.top = node.next
67+
return node.data

0 commit comments

Comments
 (0)