mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Implement Linked Queue and Linked Stack data structures (#1324)
* Add LinkedQueue * Add LinkedStack
This commit is contained in:
parent
e177198177
commit
7376addcd5
74
data_structures/queue/linked_queue.py
Normal file
74
data_structures/queue/linked_queue.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
""" A Queue using a Linked List like structure """
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class Node:
|
||||||
|
def __init__(self, data: Any, next: Optional["Node"] = None):
|
||||||
|
self.data: Any = data
|
||||||
|
self.next: Optional["Node"] = next
|
||||||
|
|
||||||
|
|
||||||
|
class LinkedQueue:
|
||||||
|
"""
|
||||||
|
Linked List Queue implementing put (to end of queue),
|
||||||
|
get (from front of queue) and is_empty
|
||||||
|
|
||||||
|
>>> queue = LinkedQueue()
|
||||||
|
>>> queue.is_empty()
|
||||||
|
True
|
||||||
|
>>> queue.put(5)
|
||||||
|
>>> queue.put(9)
|
||||||
|
>>> queue.put('python')
|
||||||
|
>>> queue.is_empty();
|
||||||
|
False
|
||||||
|
>>> queue.get()
|
||||||
|
5
|
||||||
|
>>> queue.put('algorithms')
|
||||||
|
>>> queue.get()
|
||||||
|
9
|
||||||
|
>>> queue.get()
|
||||||
|
'python'
|
||||||
|
>>> queue.get()
|
||||||
|
'algorithms'
|
||||||
|
>>> queue.is_empty()
|
||||||
|
True
|
||||||
|
>>> queue.get()
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
IndexError: get from empty queue
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.front: Optional[Node] = None
|
||||||
|
self.rear: Optional[Node] = None
|
||||||
|
|
||||||
|
def is_empty(self) -> bool:
|
||||||
|
""" returns boolean describing if queue is empty """
|
||||||
|
return self.front is None
|
||||||
|
|
||||||
|
def put(self, item: Any) -> None:
|
||||||
|
""" append item to rear of queue """
|
||||||
|
node: Node = Node(item)
|
||||||
|
if self.is_empty():
|
||||||
|
# the queue contains just the single element
|
||||||
|
self.front = node
|
||||||
|
self.rear = node
|
||||||
|
else:
|
||||||
|
# not empty, so we add it to the rear of the queue
|
||||||
|
assert isinstance(self.rear, Node)
|
||||||
|
self.rear.next = node
|
||||||
|
self.rear = node
|
||||||
|
|
||||||
|
def get(self) -> Any:
|
||||||
|
""" returns and removes item at front of queue """
|
||||||
|
if self.is_empty():
|
||||||
|
raise IndexError("get from empty queue")
|
||||||
|
else:
|
||||||
|
# "remove" element by having front point to the next one
|
||||||
|
assert isinstance(self.front, Node)
|
||||||
|
node: Node = self.front
|
||||||
|
self.front = node.next
|
||||||
|
if self.front is None:
|
||||||
|
self.rear = None
|
||||||
|
|
||||||
|
return node.data
|
67
data_structures/stacks/linked_stack.py
Normal file
67
data_structures/stacks/linked_stack.py
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
""" A Stack using a Linked List like structure """
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class Node:
|
||||||
|
def __init__(self, data: Any, next: Optional["Node"] = None):
|
||||||
|
self.data: Any = data
|
||||||
|
self.next: Optional["Node"] = next
|
||||||
|
|
||||||
|
|
||||||
|
class LinkedStack:
|
||||||
|
"""
|
||||||
|
Linked List Stack implementing push (to top),
|
||||||
|
pop (from top) and is_empty
|
||||||
|
|
||||||
|
>>> stack = LinkedStack()
|
||||||
|
>>> stack.is_empty()
|
||||||
|
True
|
||||||
|
>>> stack.push(5)
|
||||||
|
>>> stack.push(9)
|
||||||
|
>>> stack.push('python')
|
||||||
|
>>> stack.is_empty();
|
||||||
|
False
|
||||||
|
>>> stack.pop()
|
||||||
|
'python'
|
||||||
|
>>> stack.push('algorithms')
|
||||||
|
>>> stack.pop()
|
||||||
|
'algorithms'
|
||||||
|
>>> stack.pop()
|
||||||
|
9
|
||||||
|
>>> stack.pop()
|
||||||
|
5
|
||||||
|
>>> stack.is_empty()
|
||||||
|
True
|
||||||
|
>>> stack.pop()
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
IndexError: pop from empty stack
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.top: Optional[Node] = None
|
||||||
|
|
||||||
|
def is_empty(self) -> bool:
|
||||||
|
""" returns boolean describing if stack is empty """
|
||||||
|
return self.top is None
|
||||||
|
|
||||||
|
def push(self, item: Any) -> None:
|
||||||
|
""" append item to top of stack """
|
||||||
|
node: Node = Node(item)
|
||||||
|
if self.is_empty():
|
||||||
|
self.top = node
|
||||||
|
else:
|
||||||
|
# each node points to the item "lower" in the stack
|
||||||
|
node.next = self.top
|
||||||
|
self.top = node
|
||||||
|
|
||||||
|
def pop(self) -> Any:
|
||||||
|
""" returns and removes item at top of stack """
|
||||||
|
if self.is_empty():
|
||||||
|
raise IndexError("pop from empty stack")
|
||||||
|
else:
|
||||||
|
# "remove" element by having top point to the next one
|
||||||
|
assert isinstance(self.top, Node)
|
||||||
|
node: Node = self.top
|
||||||
|
self.top = node.next
|
||||||
|
return node.data
|
Loading…
Reference in New Issue
Block a user