https://github.com/icaoberg/python-stack
Just a simple naive implementation in Python
https://github.com/icaoberg/python-stack
data-structures education python
Last synced: about 1 month ago
JSON representation
Just a simple naive implementation in Python
- Host: GitHub
- URL: https://github.com/icaoberg/python-stack
- Owner: icaoberg
- Created: 2018-12-30T05:35:33.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2026-05-03T20:30:17.000Z (about 2 months ago)
- Last Synced: 2026-05-03T22:14:43.576Z (about 2 months ago)
- Topics: data-structures, education, python
- Language: Python
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# python-stack
> [!WARNING]
> This implementation is inspired by a homework assignment from [15-213](https://www.cs.cmu.edu/~213/) at Carnegie Mellon University. It is intended for educational purposes only and is not suitable for production use.
[](https://github.com/icaoberg/python-stack/actions/workflows/ci.yml)
[](https://github.com/icaoberg/python-stack)
[](https://github.com/icaoberg/python-stack/issues)
[](https://github.com/icaoberg/python-stack/network)
[](https://github.com/icaoberg/python-stack/stargazers)
[](https://www.gnu.org/licenses/quick-guide-gplv3.en.html)
A simple naive implementation of a [stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) in Python.
The purpose of this repo is to serve as an example of how to set up a GitHub Actions workflow.
## Definition
> A collection of items in which only the most recently added item may be removed. The latest added item is at the top. Basic operations are push and pop. Also known as "last-in, first-out" or LIFO.
— Paul E. Black, *[stack](https://xlinux.nist.gov/dads/HTML/stack.html)*, Dictionary of Algorithms and Data Structures [online], NIST.
## When to Use
A stack is the right structure any time you need to reverse order or track state that must be unwound in reverse:
- **Function call management** — every programming language runtime uses a call stack to track active function calls and local variables.
- **Undo/redo** — text editors and drawing applications push actions onto a stack so they can be popped off in reverse order on undo.
- **Expression parsing** — compilers and calculators use stacks to evaluate arithmetic expressions and match parentheses.
- **Depth-first search (DFS)** — graph traversals use a stack (explicit or via recursion) to explore as deep as possible before backtracking.
- **Browser history** — the back button works by popping the most recently visited page off a stack.
## Requirements
- Python 3.6+
## Installation
Clone the repository and install dependencies:
```bash
git clone https://github.com/icaoberg/python-stack.git
cd python-stack
pip install -r requirements.txt
```
## Usage
```python
from Stack import Stack
s = Stack()
s.push(1)
s.push(2)
s.push(3)
print(s.size()) # 3
print(s.peek()) # 3
print(s.pop()) # 3
print(s.size()) # 2
print(s.is_empty()) # False
print(s.tolist()) # [1, 2]
```
### Methods
| Method | Description |
|--------|-------------|
| `push(element)` | Add an element to the top of the stack |
| `pop()` | Remove and return the top element |
| `peek()` | Return the top element without removing it |
| `is_empty()` | Return `True` if the stack has no elements |
| `size()` | Return the number of elements in the stack |
| `tolist()` | Return a copy of the stack as a list |
## Testing
```bash
pytest tests.py
```
## Support
If you found this project helpful, consider buying me a coffee!
[](https://www.buymeacoffee.com/icaoberg)
Copyright © [icaoberg](https://github.com/icaoberg) at [Carnegie Mellon University](https://www.cmu.edu). All rights reserved.