-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLinkedList.py
174 lines (98 loc) · 3.01 KB
/
LinkedList.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class Node:
def __init__(self, value = None, next = None):
self.value = value
self.next = next
def __str__(self):
return(str(self.value))
mynode1 = Node("1")
mynode2 = Node("2")
mynode3 = Node("3")
mynode1.next = mynode2
mynode2.next = mynode3
def Iterate(top):
while(top != None):
print(top.Value)
top = top.next
# End While
# End Iterate
def FindCell(top, target):
while(top != None):
if(top.value == target):
return (top)
top = top.next
# End While
return None
# End FindCell
def FindCellBefore(top, target):
if (top == None):
return None
# Search for the target Value
while(top.next != None):
if(top.next.Value2 == target):
return (top)
top = top.next
# End While
# If we get this far, the target is not in the list
return None
# End FindCellBefore
# Modifiying the last code with usage of sentinel
def FindCellBefore(top, target):
# Search for the target value
while(top.next != None):
if (top.next.value == target):
return (top)
top = top.next
# End While
# If we get this far, the target is not in the list
return None
# End FindCellBefore
'''
Following algorithm adds a new item at the beginning of a linked list
'''
def AddAtBeginnig(top, new_cell):
new_cell.next = top.next
top.next = new_cell
# End AddAtBeginning
'''
Following algorithm adds a new item at the end of a linked list
'''
def AddAtEnd(top, new_cell):
# Find the last Cell.
while(top.next != None):
top = top.next
# End wHile
# Adds the new cell at the end
top.next = new_cell
new_cell.next = None
# End AddAtEnd
'''
If we want to insert and item after specific cell
'''
def InsertCell(after_me, new_cell):
new_cell.next = after_me.next
after_me.next = new_cell
# End InsertCell
'''
Languages like java does not need to erase free cells
they are using garbage collection system.
If your language has something like garbage collection system
use this function, otherwise next function will erase the free cells/
'''
def DeleteAfter(after_me):
after_me.next = after_me.next.next
# End DeleteAfter
def DeleteAfter(after_me):
target_cell = after_me.next
after_me.next = after_me.next.next
free(target_cell)#?
# End DeleteAfter
def DestroyList(top):
while(top != None):
# Save a Pointer to the next Cell
next_cell = top.next
# Free top.
free(top) #?
# move to the next cell
top = next_cell
# End While
# End DestroyList