-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.py
More file actions
108 lines (87 loc) · 2.24 KB
/
Stack.py
File metadata and controls
108 lines (87 loc) · 2.24 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from DoublyLinkedList import DoublyLinkedList
class Stack(DoublyLinkedList):
__first = None
__stack = None
__size = 0
def __init__(self, first=None) -> None:
if first != None:
element = DoublyLinkedList(first)
self.__first = element
self.__stack = element
self.__size = 1
def peak(self):
'''
# Returns:
Top of the Stack.
'''
return (self.__stack.llist) if self.__size > 0 else None
def push(self, element) -> None:
'''
Push an element to the back of the Stack.
# Params:
element - Can be of any type, but will be placed in a container. The container will be
placed at the top of the Stack.
'''
elem = DoublyLinkedList(element)
if self.__size == 0:
self.__first = elem
self.__stack = elem
else:
self.__stack.append = elem
self.__stack = self.__stack.getNext
self.__size += 1
def pop(self) -> DoublyLinkedList | None:
'''
Removes the element at the top of the stack.
# Returns:
None - If the stack is empty before or after the pop\n
or\n
DoublyLinkedList - The new element that is at the top of the Stack.
'''
if self.__size > 0:
if self.__stack.getPrev == None:
del self.__stack
del self.__first
self.__stack, self.__first = None, None
else:
temp = self.__stack.getPrev
del self.__stack
self.__stack = temp
self.__size -= 1
return self.__stack
def clear(self) -> None:
while self.__size > 0:
self.pop()
def contains(self, element) -> bool:
'''
Checks if the Stack has a given element
# Params:
element - Check if this element is in the Stack
# Returns:
True if element is in the Stack else False
'''
if self.__size > 0:
PRIMITIVES = (int, bool, float, complex)
current = self.__first
if isinstance(element, PRIMITIVES):
while current != None:
if current.llist == element:
return True
current = current.getNext
else:
while current != None:
if current.llist.__dict__ == element.__dict__:
return True
current = current.getNext
return False
def insertBefore(self, first: DoublyLinkedList) -> None:
pass
def isEmpty(self) -> bool:
return (self.__size > 0)
@property
def size(self) -> int:
'''
# Returns:
The number of elements in the Stack.
'''
return self.__size