mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
71
ebook_src/abstract_structures/stacks/linked_stack.py
Normal file
71
ebook_src/abstract_structures/stacks/linked_stack.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
""" A stack made of linked list"""
|
||||
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, value=None, pointer=None):
|
||||
self.value = value
|
||||
self.pointer = pointer
|
||||
|
||||
|
||||
class Stack(object):
|
||||
def __init__(self):
|
||||
self.head = None
|
||||
|
||||
def isEmpty(self):
|
||||
return not bool(self.head)
|
||||
|
||||
def push(self, item):
|
||||
self.head = Node(item, self.head)
|
||||
|
||||
|
||||
def pop(self):
|
||||
if self.head:
|
||||
node = self.head
|
||||
self.head = node.pointer
|
||||
return node.value
|
||||
else:
|
||||
print('Stack is empty.')
|
||||
|
||||
|
||||
def peek(self):
|
||||
if self.head:
|
||||
return self.head.value
|
||||
else:
|
||||
print('Stack is empty.')
|
||||
|
||||
|
||||
def size(self):
|
||||
node = self.head
|
||||
count = 0
|
||||
while node:
|
||||
count +=1
|
||||
node = node.pointer
|
||||
return count
|
||||
|
||||
|
||||
def _printList(self):
|
||||
node = self.head
|
||||
while node:
|
||||
print(node.value)
|
||||
node = node.pointer
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
stack = Stack()
|
||||
print("Is the stack empty? ", stack.isEmpty())
|
||||
print("Adding 0 to 10 in the stack...")
|
||||
for i in range(10):
|
||||
stack.push(i)
|
||||
stack._printList()
|
||||
print("Stack size: ", stack.size())
|
||||
print("Stack peek : ", stack.peek())
|
||||
print("Pop...", stack.pop())
|
||||
print("Stack peek: ", stack.peek())
|
||||
print("Is the stack empty? ", stack.isEmpty())
|
||||
stack._printList()
|
Loading…
Add table
Add a link
Reference in a new issue