Skip to content

Commit

Permalink
stack and queue as linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
Anurag Saini committed Sep 7, 2024
1 parent 110c249 commit 5d42da6
Show file tree
Hide file tree
Showing 43 changed files with 1,004 additions and 306 deletions.
87 changes: 87 additions & 0 deletions docs/data-structures/linked-list/queue-as-linked-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Linked list as Queue

<style>
.md-logo img {
content: url('/data-structures/linked-list/polyline-light.svg');
}

:root [data-md-color-scheme=slate] .md-logo img {
content: url('/data-structures/linked-list/polyline-night.svg');
}
</style>

## Implementation

Singly-linked list is not efficient for queue implementation. That's because `enqueue` can happen in \\(O(1)\\) at `head`, but then `dequeue` would require traversing the whole list till reaching the predecessor of `tail`. i.e. \\(O(n)\\).

```python linenums="1" title="queue.py"
from linkedlist.list import LinkedList


class Queue:

def __init__(self):
self._list = LinkedList()

@property
def empty(self): return self._list.empty

@property
def peek(self): return self._list.head

def enqueue(self, data: int):
self._list.append(data)

def dequeue(self):
return self._list.remove_front()

```

## Unit tests

```python linenums="1" title="test_queue.py"
from linkedlist.queue import Queue


def test_queue_initialization():
queue = Queue()
assert queue.empty
assert queue.peek is None


def test_queue_enqueue():
queue = Queue()
assert queue.empty

queue.enqueue(1)
assert queue.peek == 1
assert not queue.empty

queue.enqueue(2)
assert queue.peek == 1
assert not queue.empty

queue.enqueue(3)
assert queue.peek == 1
assert not queue.empty


def test_queue_dequeue():
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

assert queue.dequeue() == 1
assert not queue.empty

assert queue.dequeue() == 2
assert not queue.empty

assert queue.dequeue() == 3
assert queue.empty

assert queue.dequeue() is None
assert queue.empty

```
14 changes: 14 additions & 0 deletions docs/data-structures/linked-list/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ Unlike the previous iteration of linked list, this time we don't have `head` and
@property
def empty(self):
return self._sentinel.next == self._sentinel

// Peak head
@property
def head(self) -> Optional[int]:
if self.empty:
return None
return self._sentinel.next.value

// Peak tail
@property
def tail(self) -> Optional[int]:
if self.empty:
return None
return self._sentinel.prev.value
```

## Adding elements
Expand Down
94 changes: 94 additions & 0 deletions docs/data-structures/linked-list/stack-as-linked-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Linked list as Stack

<style>
.md-logo img {
content: url('/data-structures/linked-list/polyline-light.svg');
}

:root [data-md-color-scheme=slate] .md-logo img {
content: url('/data-structures/linked-list/polyline-night.svg');
}
</style>

## Implementation

This can be implemented even as a singly-linked list, with `push` and `pop` happening at `head`.

```python linenums="1" title="stack.py"
from typing import Optional

from linkedlist.list import LinkedList


class Stack:

def __init__(self):
self._list = LinkedList()

@property
def empty(self): return self._list.empty

@property
def top(self) -> Optional[int]:
return self._list.head

def push(self, data: int):
self._list.prepend(data)

def pop(self) -> Optional[int]:
if self.empty:
return None
return self._list.remove_front()

```

## Unit tests

```python linenums="1" title="test_stack.py"
import pytest
from stack.stack import Stack
from typing import Optional


def test_initialize_stack():
s = Stack()
assert s.empty is True


def test_push_element_to_stack():
s = Stack()
assert s.top is None
assert s.empty

s.push(3)
assert s.top is 3
assert not s.empty

s.push(2)
assert s.top is 2
assert not s.empty

s.push(1)
assert s.top is 1
assert not s.empty


def test_pop_element_from_stack():
s = Stack()
s.push(3)
s.push(2)
s.push(1)

assert s.pop() is 1
assert not s.empty

assert s.pop() is 2
assert not s.empty

assert s.pop() is 3
assert s.empty

assert s.pop() is None
assert s.empty

```
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ nav:
- Basics: 'data-structures/linked-list/basics.md'
- Reversing linked list: 'data-structures/linked-list/reverse.md'
- Sentinel: 'data-structures/linked-list/sentinel.md'
- Linked list as Stack: 'data-structures/linked-list/stack-as-linked-list.md'
- Linked list as Queue: 'data-structures/linked-list/queue-as-linked-list.md'
- Heap: 'data-structures/heap/index.md'
- Circular list: 'data-structures/circular-list.md'
- Graph:
Expand Down
8 changes: 2 additions & 6 deletions site/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<nav class="md-header__inner md-grid" aria-label="Header">
<a href="/." title="listless 💤" class="md-header__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="/assets/tired.svg" alt="logo">
<img src="/assets/fatigue-light.svg" alt="logo">

</a>
<label class="md-header__button md-icon" for="__drawer">
Expand Down Expand Up @@ -166,7 +166,7 @@
<label class="md-nav__title" for="__drawer">
<a href="/." title="listless 💤" class="md-nav__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="/assets/tired.svg" alt="logo">
<img src="/assets/fatigue-light.svg" alt="logo">

</a>
listless 💤
Expand Down Expand Up @@ -1653,10 +1653,6 @@ <h1>404 - Not found</h1>

<script src="/assets/javascripts/bundle.fe8b6f2b.min.js"></script>

<script src="/javascripts/mathjax.js"></script>

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>


Expand Down
8 changes: 2 additions & 6 deletions site/algorithms/partition/partition/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<nav class="md-header__inner md-grid" aria-label="Header">
<a href="../../.." title="listless 💤" class="md-header__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../../assets/tired.svg" alt="logo">
<img src="../../../assets/fatigue-light.svg" alt="logo">

</a>
<label class="md-header__button md-icon" for="__drawer">
Expand Down Expand Up @@ -184,7 +184,7 @@
<label class="md-nav__title" for="__drawer">
<a href="../../.." title="listless 💤" class="md-nav__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../../assets/tired.svg" alt="logo">
<img src="../../../assets/fatigue-light.svg" alt="logo">

</a>
listless 💤
Expand Down Expand Up @@ -2212,10 +2212,6 @@ <h2 id="three-way-partition">Three-way partition</h2>

<script src="../../../assets/javascripts/bundle.fe8b6f2b.min.js"></script>

<script src="../../../javascripts/mathjax.js"></script>

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>


Expand Down
8 changes: 2 additions & 6 deletions site/algorithms/search/binary/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<nav class="md-header__inner md-grid" aria-label="Header">
<a href="../../.." title="listless 💤" class="md-header__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../../assets/tired.svg" alt="logo">
<img src="../../../assets/fatigue-light.svg" alt="logo">

</a>
<label class="md-header__button md-icon" for="__drawer">
Expand Down Expand Up @@ -184,7 +184,7 @@
<label class="md-nav__title" for="__drawer">
<a href="../../.." title="listless 💤" class="md-nav__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../../assets/tired.svg" alt="logo">
<img src="../../../assets/fatigue-light.svg" alt="logo">

</a>
listless 💤
Expand Down Expand Up @@ -2015,10 +2015,6 @@ <h2 id="examples">Examples</h2>

<script src="../../../assets/javascripts/bundle.fe8b6f2b.min.js"></script>

<script src="../../../javascripts/mathjax.js"></script>

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>


Expand Down
8 changes: 2 additions & 6 deletions site/algorithms/sort/mergesort/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<nav class="md-header__inner md-grid" aria-label="Header">
<a href="../../.." title="listless 💤" class="md-header__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../../assets/tired.svg" alt="logo">
<img src="../../../assets/fatigue-light.svg" alt="logo">

</a>
<label class="md-header__button md-icon" for="__drawer">
Expand Down Expand Up @@ -184,7 +184,7 @@
<label class="md-nav__title" for="__drawer">
<a href="../../.." title="listless 💤" class="md-nav__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../../assets/tired.svg" alt="logo">
<img src="../../../assets/fatigue-light.svg" alt="logo">

</a>
listless 💤
Expand Down Expand Up @@ -2152,10 +2152,6 @@ <h2 id="benchmark">Benchmark</h2>

<script src="../../../assets/javascripts/bundle.fe8b6f2b.min.js"></script>

<script src="../../../javascripts/mathjax.js"></script>

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>


Expand Down
8 changes: 2 additions & 6 deletions site/algorithms/sort/quicksort/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<nav class="md-header__inner md-grid" aria-label="Header">
<a href="../../.." title="listless 💤" class="md-header__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../../assets/tired.svg" alt="logo">
<img src="../../../assets/fatigue-light.svg" alt="logo">

</a>
<label class="md-header__button md-icon" for="__drawer">
Expand Down Expand Up @@ -184,7 +184,7 @@
<label class="md-nav__title" for="__drawer">
<a href="../../.." title="listless 💤" class="md-nav__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../../assets/tired.svg" alt="logo">
<img src="../../../assets/fatigue-light.svg" alt="logo">

</a>
listless 💤
Expand Down Expand Up @@ -2117,10 +2117,6 @@ <h2 id="benchmark">Benchmark</h2>

<script src="../../../assets/javascripts/bundle.fe8b6f2b.min.js"></script>

<script src="../../../javascripts/mathjax.js"></script>

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>


Expand Down
8 changes: 2 additions & 6 deletions site/android/activities/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<nav class="md-header__inner md-grid" aria-label="Header">
<a href="../.." title="listless 💤" class="md-header__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../assets/tired.svg" alt="logo">
<img src="../../assets/fatigue-light.svg" alt="logo">

</a>
<label class="md-header__button md-icon" for="__drawer">
Expand Down Expand Up @@ -184,7 +184,7 @@
<label class="md-nav__title" for="__drawer">
<a href="../.." title="listless 💤" class="md-nav__button md-logo" aria-label="listless 💤" data-md-component="logo">

<img src="../../assets/tired.svg" alt="logo">
<img src="../../assets/fatigue-light.svg" alt="logo">

</a>
listless 💤
Expand Down Expand Up @@ -1919,10 +1919,6 @@ <h2 id="lifecycle">Lifecycle</h2>

<script src="../../assets/javascripts/bundle.fe8b6f2b.min.js"></script>

<script src="../../javascripts/mathjax.js"></script>

<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>


Expand Down
Loading

0 comments on commit 5d42da6

Please sign in to comment.