Last active
May 25, 2017 06:44
-
-
Save mortymacs/996cbe617d11ff3b0f365d0e2d1af8f5 to your computer and use it in GitHub Desktop.
Simple Stack in Python (Last-in-First-out)
This file contains hidden or 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
| class Stack: | |
| def __init__(self): | |
| self._items = [] | |
| def push(self, data): | |
| self._items.insert(0, data) | |
| def pop(self): | |
| return self._items.pop(0) | |
| def peek(self): | |
| return self._items[0] | |
| def size(self): | |
| return len(self._items) | |
| def is_empty(self): | |
| return self._items == [] | |
| def stack_list(self): | |
| return self._items | |
| a = Stack() | |
| print("Is empty: ", a.is_empty()) | |
| # Is empty: True | |
| a.push("D1") | |
| a.push("D2") | |
| a.push("D3") | |
| print("Stack list: ", a.stack_list()) | |
| # Stack list: ['D3', 'D2', 'D1'] | |
| print("Stack size: ", a.size()) | |
| # Stack size: 3 | |
| print("Pop item: ", a.pop()) | |
| # Pop item: D3 | |
| print("Pop item: ", a.pop()) | |
| # Pop item: D2 | |
| print("Stack list: ", a.stack_list()) | |
| # Stack list: ['D1'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment