forked from algorithm-archivists/algorithm-archive
-
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.
Added a stack and queue implementation for python3 (algorithm-archivi…
…sts#1008) Co-authored-by: Michael CC <[email protected]>
- Loading branch information
Showing
4 changed files
with
98 additions
and
1 deletion.
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
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,45 @@ | ||
#!/usr/bin/env python3 | ||
|
||
__author__ = "Michael Ciccotosto-Camp" | ||
|
||
from typing import TypeVar, Generic | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class Queue(Generic[T]): | ||
def __init__(self) -> None: | ||
self.__list: list[T] = list() | ||
|
||
def dequeue(self) -> T: | ||
return self.__list.pop(0) | ||
|
||
def enqueue(self, element: T) -> int: | ||
self.__list.append(element) | ||
return len(self) | ||
|
||
def front(self) -> T: | ||
return self.__list[0] | ||
|
||
def __len__(self) -> int: | ||
return len(self.__list) | ||
|
||
def __str__(self) -> str: | ||
return str(self.__list) | ||
|
||
|
||
def main() -> None: | ||
int_queue: Queue[int] = Queue() | ||
|
||
int_queue.enqueue(4) | ||
int_queue.enqueue(5) | ||
int_queue.enqueue(9) | ||
|
||
print(int_queue.dequeue()) | ||
print(len(int_queue)) | ||
print(int_queue.front()) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,45 @@ | ||
#!/usr/bin/env python3 | ||
|
||
__author__ = "Michael Ciccotosto-Camp" | ||
|
||
from typing import TypeVar, Generic | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class Stack(Generic[T]): | ||
def __init__(self) -> None: | ||
self.__list: list[T] = [] | ||
|
||
def pop(self) -> T: | ||
return self.__list.pop() | ||
|
||
def push(self, element: T) -> int: | ||
self.__list.append(element) | ||
return len(self) | ||
|
||
def top(self) -> T: | ||
return self.__list[-1] | ||
|
||
def __len__(self) -> int: | ||
return len(self.__list) | ||
|
||
def __str__(self) -> str: | ||
return str(self.__list) | ||
|
||
|
||
def main() -> None: | ||
int_stack: Stack[int] = Stack() | ||
|
||
int_stack.push(4) | ||
int_stack.push(5) | ||
int_stack.push(9) | ||
|
||
print(int_stack.pop()) | ||
print(len(int_stack)) | ||
print(int_stack.top()) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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