-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinked Stack.py
70 lines (58 loc) · 1.99 KB
/
Linked Stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Node:
"""Class to represent a node in Python3"""
def __init__(self, data):
self.data = data # Node value
self.next = None # Next node
class LinkedStack:
"""Class to represent a linked stack in Python3"""
def __init__(self):
self._top = None # Top of stack
self._size = 0 # The stack size
def push(self, elem):
"""Adds an element at the top of a stack"""
if self._size == 0:
self._top = Node(elem)
else:
aux = self._top
self._top = Node(elem)
self._top.next = aux
self._size += 1
def pop(self):
"""Removes the topmost element of a stack"""
if self._size == 0:
raise Exception("Empty stack")
elem, self._top = self._top.data, self._top.next
self._size -= 1
return elem
def top(self):
"""Returns the element on the top of the stack but does not remove it"""
if self._size == 0:
raise Exception("Empty stack")
return self._top.data
def empty(self):
"""Returns true if the stack is empty, otherwise, it returns false"""
if self._size == 0:
return True
return False
def length(self):
"""Returns the size of stack"""
return self._size
def __del__(self):
"""Destructor method"""
def __str__(self):
"""Method for representing the stack, excluding NoneType objects (user)"""
rep = "\033[1;34m" + "top -> " + "\033[0;0m"
if self._size == 0:
rep += "None"
return rep
pointer = self._top
for i in range(self._size):
if i == 0:
rep += f"{str(pointer.data).rjust(2)}"
else:
rep += f"\n{str(pointer.data).rjust(10)}"
pointer = pointer.next
return rep
def __repr__(self):
"""Method for representing the stack, excluding NoneType objects (developer)"""
return str(self)