mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 17:01:28 -04:00
Change the dir structure slightly
This commit is contained in:
parent
6b6fe21db3
commit
2f4a9638c0
184 changed files with 0 additions and 21 deletions
|
@ -1,71 +0,0 @@
|
|||
#!/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