-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_structure.py
More file actions
95 lines (72 loc) · 2.17 KB
/
data_structure.py
File metadata and controls
95 lines (72 loc) · 2.17 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
"""
Data structure implementation
"""
class Stack(object):
def __init__(self):
self.__data = []
@property
def is_empty(self):
return len(self.__data) == 0
@property
def size(self):
return len(self.__data)
def push(self, value):
self.__data.append(value)
def pop(self):
if len(self.__data) <= 0:
raise IndexError('No data in stack')
value = self.__data[len(self.__data) - 1]
del self.__data[len(self.__data) - 1]
return value
class Node(object):
def __init__(self, value, next_node=None):
self.__data = value
self.__next_node = next_node
def get(self):
return self.__data
def get_next_node(self):
return self.__next_node
def set_next_node(self, next_node):
self.__next_node = next_node
class LinkedList(object):
def __init__(self, head=None):
self.__head = head
@property
def size(self):
current = self.__head
count = 0
while current:
count = count + 1
current = current.get_next_node()
return count
def insert(self, value):
new_node = Node(value)
new_node.set_next_node(self.__head)
self.__head = new_node
def search(self, value):
current = self.__head
found = False
while current and found is False:
if current.get() == value:
found = True
else:
current = current.get_next_node()
if current is None:
raise ValueError("Value is not in Linked List")
return current
def delete(self, value):
current = self.__head
previous = None
found = False
while current and found is False:
if current.get() == value:
found = True
else:
previous = current
current = current.get_next_node()
if current is None:
raise ValueError('Value is not in Linked List')
if previous is None:
self.head = current.get_next_node()
else:
previous.set_next_node(current.get_next_node())