forked from wangzheng0822/algo
-
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.
implementation of DynamicArray and queue based upon linked list in py…
…thon
- Loading branch information
1 parent
01ae89d
commit 1c6b5e9
Showing
2 changed files
with
109 additions
and
0 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,51 @@ | ||
""" | ||
Author: Wenru | ||
""" | ||
|
||
from typing import Optional | ||
|
||
class DynamicArrayQueue: | ||
|
||
def __init__(self, capacity: int): | ||
self._items = [] | ||
self._capacity = capacity | ||
self._head = 0 | ||
self._tail = 0 | ||
|
||
def enqueue(self, item: str) -> bool: | ||
if self._tail == self._capacity: | ||
if self._head == 0: return False | ||
|
||
self._items[0 : self._tail - self._head] = self._items[self._head : self._tail] | ||
self._tail -= self._head | ||
self._head = 0 | ||
|
||
if self._tail == len(self._items): | ||
self._items.append(item) | ||
else: | ||
self._items[self._tail] = item | ||
self._tail += 1 | ||
return True | ||
|
||
def dequeue(self) -> Optional[str]: | ||
if self._head != self._tail: | ||
item = self._items[self._head] | ||
self._head += 1 | ||
return item | ||
|
||
def __repr__(self) -> str: | ||
return " ".join(item for item in self._items[self._head:self._tail]) | ||
|
||
if __name__ == "__main__": | ||
q = DynamicArrayQueue(10) | ||
for i in range(10): | ||
q.enqueue(str(i)) | ||
print(q) | ||
|
||
for _ in range(3): | ||
q.dequeue() | ||
print(q) | ||
|
||
q.enqueue("7") | ||
q.enqueue("8") | ||
print(q) |
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,58 @@ | ||
""" | ||
Queue based upon linked list | ||
Author: Wenru | ||
""" | ||
|
||
from typing import Optional | ||
|
||
class Node: | ||
|
||
def __init__(self, data: str, next=None): | ||
self.data = data | ||
self._next = next | ||
|
||
class LinkedQueue: | ||
|
||
def __init__(self): | ||
self._head: Optional[Node] = None | ||
self._tail: Optional[Node] = None | ||
|
||
def enqueue(self, value: str): | ||
new_node = Node(value) | ||
if self._tail: | ||
self._tail._next = new_node | ||
else: | ||
self._head = new_node | ||
self._tail = new_node | ||
|
||
def dequeue(self) -> Optional[str]: | ||
if self._head: | ||
value = self._head.data | ||
self._head = self._head._next | ||
if not self._head: | ||
self._tail = None | ||
return value | ||
|
||
def __repr__(self) -> str: | ||
values = [] | ||
current = self._head | ||
while current: | ||
values.append(current.data) | ||
current = current._next | ||
return "->".join(value for value in values) | ||
|
||
|
||
if __name__ == "__main__": | ||
q = LinkedQueue() | ||
for i in range(10): | ||
q.enqueue(str(i)) | ||
print(q) | ||
|
||
for _ in range(3): | ||
q.dequeue() | ||
print(q) | ||
|
||
q.enqueue("7") | ||
q.enqueue("8") | ||
print(q) |