Skip to content

Commit

Permalink
Added a stack and queue implementation for python3 (algorithm-archivi…
Browse files Browse the repository at this point in the history
…sts#1008)

Co-authored-by: Michael CC <[email protected]>
  • Loading branch information
Michae1CC and Michael CC authored Mar 30, 2023
1 parent 3753576 commit 5aabe09
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- K. Shudipto Amin
- Peanutbutter_Warrior
- Thijs Raymakers
- Michael Ciccotosto-Camp
45 changes: 45 additions & 0 deletions contents/stacks_and_queues/code/python/queue.py
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()
45 changes: 45 additions & 0 deletions contents/stacks_and_queues/code/python/stack.py
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()
8 changes: 7 additions & 1 deletion contents/stacks_and_queues/stacks_and_queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ In *stacks*, data follows *Last In, First Out* (LIFO), which basically means tha
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.

For the most part, though, queues and stacks are treated the same way. There must be a way to:

1. look at the first element (`top()`)
2. to remove the first element (`pop()`)
3. to push elements onto the data structure (`push()`)

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.

## Example Code

Here is a simple implementation of a stack:
{% method %}
{% sample lang="ts" %}
Expand All @@ -24,6 +26,8 @@ Here is a simple implementation of a stack:
[import, lang:"cpp"](code/cpp/stack.cpp)
{% sample lang="rust" %}
[import, lang:"rust"](code/rust/Stack.rs)
{% sample lang="python" %}
[import, lang:"python"](code/python/stack.py)
{% endmethod %}

Here is a simple implementation of a queue:
Expand All @@ -36,9 +40,10 @@ Here is a simple implementation of a queue:
[import, lang:"cpp"](code/cpp/queue.cpp)
{% sample lang="rust" %}
[import, lang:"rust" ](code/rust/Queue.rs)
{% sample lang="python" %}
[import, lang:"python"](code/python/queue.py)
{% endmethod %}


## License

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

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:

- none

0 comments on commit 5aabe09

Please sign in to comment.