mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-24 01:11:35 -04:00
fixes
This commit is contained in:
parent
aa5010594e
commit
7b9e953f5a
194 changed files with 28 additions and 34 deletions
52
src/abstract_structures/adt/stacks/linked_stack.py
Normal file
52
src/abstract_structures/adt/stacks/linked_stack.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
class Node:
|
||||
def __init__(self, value=None):
|
||||
self.value = value
|
||||
self.next = None
|
||||
|
||||
class StackwithNodes:
|
||||
''' Define a Stack with nodes'''
|
||||
def __init__(self):
|
||||
self.top = None
|
||||
|
||||
def isEmpty(self):
|
||||
return bool(self.top)
|
||||
|
||||
def pop(self):
|
||||
node = self.top
|
||||
self.top = node.next
|
||||
return node.value
|
||||
|
||||
def push(self, value):
|
||||
node = Node(value)
|
||||
node.next = self.top
|
||||
self.top = node
|
||||
|
||||
def size(self):
|
||||
node = self.top
|
||||
if node not None: num_nodes = 1
|
||||
else: return 0
|
||||
while node.next:
|
||||
num_nodes += 1
|
||||
node = node.next
|
||||
return num_nodes
|
||||
|
||||
def peek(self):
|
||||
return self.top.value
|
||||
|
||||
|
||||
def main():
|
||||
stack = StackwithNodes()
|
||||
stack.push(1)
|
||||
stack.push(2)
|
||||
stack.push(3)
|
||||
print(stack.size())
|
||||
print(stack.peek())
|
||||
print(stack.pop())
|
||||
print(stack.peek())
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue