Skip to content

Implement Linked Queue and Linked Stack data structures #1324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions data_structures/queue/linked_queue.py
Original file line number Diff line number Diff line change
@@ -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 changes: 67 additions & 0 deletions data_structures/stacks/linked_stack.py
Original file line number Diff line number Diff line change
@@ -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