-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anurag Saini
committed
Sep 7, 2024
1 parent
110c249
commit 5d42da6
Showing
43 changed files
with
1,004 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.