Skip to content

Commit 5aabe09

Browse files
Michae1CCMichael CC
and
Michael CC
authored
Added a stack and queue implementation for python3 (#1008)
Co-authored-by: Michael CC <mciccoto@amazon.com>
1 parent 3753576 commit 5aabe09

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
6464
- K. Shudipto Amin
6565
- Peanutbutter_Warrior
6666
- Thijs Raymakers
67+
- Michael Ciccotosto-Camp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
3+
__author__ = "Michael Ciccotosto-Camp"
4+
5+
from typing import TypeVar, Generic
6+
7+
8+
T = TypeVar("T")
9+
10+
11+
class Queue(Generic[T]):
12+
def __init__(self) -> None:
13+
self.__list: list[T] = list()
14+
15+
def dequeue(self) -> T:
16+
return self.__list.pop(0)
17+
18+
def enqueue(self, element: T) -> int:
19+
self.__list.append(element)
20+
return len(self)
21+
22+
def front(self) -> T:
23+
return self.__list[0]
24+
25+
def __len__(self) -> int:
26+
return len(self.__list)
27+
28+
def __str__(self) -> str:
29+
return str(self.__list)
30+
31+
32+
def main() -> None:
33+
int_queue: Queue[int] = Queue()
34+
35+
int_queue.enqueue(4)
36+
int_queue.enqueue(5)
37+
int_queue.enqueue(9)
38+
39+
print(int_queue.dequeue())
40+
print(len(int_queue))
41+
print(int_queue.front())
42+
43+
44+
if __name__ == "__main__":
45+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
3+
__author__ = "Michael Ciccotosto-Camp"
4+
5+
from typing import TypeVar, Generic
6+
7+
8+
T = TypeVar("T")
9+
10+
11+
class Stack(Generic[T]):
12+
def __init__(self) -> None:
13+
self.__list: list[T] = []
14+
15+
def pop(self) -> T:
16+
return self.__list.pop()
17+
18+
def push(self, element: T) -> int:
19+
self.__list.append(element)
20+
return len(self)
21+
22+
def top(self) -> T:
23+
return self.__list[-1]
24+
25+
def __len__(self) -> int:
26+
return len(self.__list)
27+
28+
def __str__(self) -> str:
29+
return str(self.__list)
30+
31+
32+
def main() -> None:
33+
int_stack: Stack[int] = Stack()
34+
35+
int_stack.push(4)
36+
int_stack.push(5)
37+
int_stack.push(9)
38+
39+
print(int_stack.pop())
40+
print(len(int_stack))
41+
print(int_stack.top())
42+
43+
44+
if __name__ == "__main__":
45+
main()

contents/stacks_and_queues/stacks_and_queues.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ In *stacks*, data follows *Last In, First Out* (LIFO), which basically means tha
77
In *Queues*, data follows *First In, First Out* (FIFO), which means that whichever element you put in first will be the first element you take out. Imagine a queue of people. It would be unfair if the first person in line for groceries were not the first person to receive attention once the attendant finally shows up.
88

99
For the most part, though, queues and stacks are treated the same way. There must be a way to:
10+
1011
1. look at the first element (`top()`)
1112
2. to remove the first element (`pop()`)
1213
3. to push elements onto the data structure (`push()`)
1314

1415
The notation for this depends on the language you are using. Queues, for example, will often use `dequeue()` instead of `pop()` and `front()` instead of `top()`. You will see the language-specific details in the source code under the algorithms in this book, so for now it's simply important to know what stacks and queues are and how to access elements held within them.
1516

1617
## Example Code
18+
1719
Here is a simple implementation of a stack:
1820
{% method %}
1921
{% sample lang="ts" %}
@@ -24,6 +26,8 @@ Here is a simple implementation of a stack:
2426
[import, lang:"cpp"](code/cpp/stack.cpp)
2527
{% sample lang="rust" %}
2628
[import, lang:"rust"](code/rust/Stack.rs)
29+
{% sample lang="python" %}
30+
[import, lang:"python"](code/python/stack.py)
2731
{% endmethod %}
2832

2933
Here is a simple implementation of a queue:
@@ -36,9 +40,10 @@ Here is a simple implementation of a queue:
3640
[import, lang:"cpp"](code/cpp/queue.cpp)
3741
{% sample lang="rust" %}
3842
[import, lang:"rust" ](code/rust/Queue.rs)
43+
{% sample lang="python" %}
44+
[import, lang:"python"](code/python/queue.py)
3945
{% endmethod %}
4046

41-
4247
## License
4348

4449
##### Code Examples
@@ -54,4 +59,5 @@ The text of this chapter was written by [James Schloss](https://github.com/leios
5459
##### Pull Requests
5560

5661
After initial licensing ([#560](https://github.com/algorithm-archivists/algorithm-archive/pull/560)), the following pull requests have modified the text or graphics of this chapter:
62+
5763
- none

0 commit comments

Comments
 (0)