forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_List.py
211 lines (155 loc) · 4.72 KB
/
Linked_List.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
class node:
def __init__(self, data):
self.data = data
self.next = None
class Linked_List:
def __init__(self):
self.head = None
def Is_List_Empty(self):
if self.head == None:
return True
return False
def Insert_At_Beginning(self, value):
temp = node(value)
temp.next = self.head
self.head = temp
def Insert_At_End(self, value):
temp = node(value)
temp.next = None
if self.Is_List_Empty():
self.head = temp
return
current = self.head
while current.next != None:
current = current.next
current.next = temp
def Insert_After_Value(self, desired, value):
current = self.head
while current != None and current.data != desired:
current = current.next
if current == None:
print("Element " + str(desired) + " not in list")
else:
temp = node(value)
temp.next = current.next
current.next = temp
def Delete_At_Beginning(self):
if self.Is_List_Empty():
print("List is empty")
else:
temp = self.head
self.head = self.head.next
temp.next = None
def Delete_At_End(self):
if self.Is_List_Empty():
print("List is empty")
return
temp = self.head
if self.head.next == None:
self.head = None
temp.next = None
return
while temp.next != None:
prev = temp
temp = temp.next
prev.next = temp.next
temp.next = None
def Delete_With_Value(self, desired):
if self.Is_List_Empty():
print("List is empty")
return
temp = self.head
if self.head.data == desired:
self.head = self.head.next
temp.next = None
return
while temp != None and temp.data != desired:
prev = temp
temp = temp.next
if temp == None:
print("Element " + str(desired) + " is not in list")
else:
prev.next = temp.next
temp.next = None
def Search(self, desired):
temp = self.head
while temp != None and temp.data != desired:
temp = temp.next
if temp == None:
print("Element " + str(desired) + " not found")
else:
print("Element " + str(desired) + " is present in list")
def Print_Linked_List(self):
if self.Is_List_Empty():
print("List is Empty")
return
current = self.head
while current.next != None:
print(current.data, end = " -> ")
current = current.next
print(current.data)
def Get_Length_Iterative(self):
current = self.head
length = 0
while current != None:
length += 1
current = current.next
return length
def Get_Length_Recursive(self):
def Count_Recursive(node):
if node is None:
return 0
else:
return 1 + Count_Recursive(node.next)
return Count_Recursive(self.head)
LinkedList = Linked_List()
print("List Created!")
LinkedList.Print_Linked_List()
print("Current Length:", LinkedList.Get_Length_Iterative())
for i in range(0, 5):
LinkedList.Insert_At_Beginning(i)
LinkedList.Print_Linked_List()
print("Current Length:", LinkedList.Get_Length_Iterative())
for i in range(5, 10):
LinkedList.Insert_At_End(i)
LinkedList.Print_Linked_List()
print("Current Length:", LinkedList.Get_Length_Recursive())
LinkedList.Insert_After_Value(5, 9)
LinkedList.Insert_After_Value(10, 9)
LinkedList.Print_Linked_List()
print("Current Length:", LinkedList.Get_Length_Iterative())
for i in range(0, 3):
LinkedList.Delete_At_End()
LinkedList.Print_Linked_List()
print("Current Length:", LinkedList.Get_Length_Recursive())
for i in range(0, 3):
LinkedList.Delete_At_Beginning()
LinkedList.Print_Linked_List()
print("Current Length:", LinkedList.Get_Length_Iterative())
LinkedList.Delete_With_Value(1)
LinkedList.Delete_With_Value(5)
LinkedList.Print_Linked_List()
print("Current Length:", LinkedList.Get_Length_Recursive())
LinkedList.Search(6)
LinkedList.Search(8)
'''
Output
List Created!
List is Empty
Current Length: 0
4 -> 3 -> 2 -> 1 -> 0
Current Length: 5
4 -> 3 -> 2 -> 1 -> 0 -> 5 -> 6 -> 7 -> 8 -> 9
Current Length: 10
Element 10 not in list
4 -> 3 -> 2 -> 1 -> 0 -> 5 -> 9 -> 6 -> 7 -> 8 -> 9
Current Length: 11
4 -> 3 -> 2 -> 1 -> 0 -> 5 -> 9 -> 6
Current Length: 8
1 -> 0 -> 5 -> 9 -> 6
Current Length: 5
0 -> 9 -> 6
Current Length: 3
Element 6 is present in list
Element 8 not found
'''