-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
47 lines (37 loc) · 1.07 KB
/
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
# Simple Stack DataStructure on Python
# Yeah it needs alot of improvement, shut up
class Node(object):
def __init__(self, value:object=None, next_node=None):
self.value = value
self.next_node = next_node
class Stack:
def __init__(self):
self.head = Node()
self.size: int = 0
def push(self, value: object):
this = self.head
self.head = Node(value)
self.head.next_node = this
self.size += 1
def pop(self) -> object:
this = self.head.value
self.head = self.head.next_node
self.size -= 1
return this
def is_empty(self):
return self.size == 0
def search(self, item: object) -> int:
this = self.head
counter = 0
if this != None:
counter += 1
while this.next_node != None:
if item == this.value:
break
counter += 1
this = this.next_node
return counter
def peek(self) -> object:
this = self.head
if this != None:
return this.value