-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinkedlist.py
More file actions
183 lines (159 loc) · 4.82 KB
/
linkedlist.py
File metadata and controls
183 lines (159 loc) · 4.82 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
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
class Node:
def __init__(self,data=None):
self.data=data
self.next=None
class LinkedList:
def __init__(self):
self.head=Node()
def addElement(self,data):
new_node=Node(data)
cur=self.head
while cur.next!=None:
cur=cur.next
cur.next=new_node
def length(self):
cur=self.head
total=0
while cur.next!=None:
total+=1
cur=cur.next
return total
def display(self):
cur_node=self.head
print("Elements are:",end=" ")
while cur_node!=None:
cur_node=cur_node.next
if cur_node!=None:
print(cur_node.data,end=" ")
print()
def addElementAtPosition(self,data,index):
new_node=Node(data)
if index>=self.length() or index<0:
self.addElement(data)
return None
cur_idx=0
cur_node=self.head
while True:
if cur_idx==index:
new_node.next=cur_node.next
cur_node.next=new_node
return
cur_node=cur_node.next
cur_idx+=1
def addElementAtHead(self,data):
new_node=Node(data)
if self.head.next==None:
self.head.next=new_node
else:
new_node.next=self.head.next
self.head.next=new_node
def addElementAtTail(self,data):
self.addElement(data)
def removeElementAtTail(self):
cur_node=self.head
while True:
if cur_node.next.next==None:
cur_node.next=None
break
cur_node=cur_node.next
def removeElementAtHead(self):
self.head.next=self.head.next.next
def removeElementAtIndex(self,index):
if index>=self.length():
self.removeElementAtTail()
return
cur_idx=0
cur_node=self.head
while True:
last_node=cur_node
cur_node=cur_node.next
if cur_idx==index:
last_node.next=cur_node.next
return
cur_idx+=1
def get(self,index):
if index>=self.length():
print("ERROR:'get' index out of range")
return None
cur_idx=0
cur_node=self.head
while True:
cur_node=cur_node.next
if cur_idx==index:
return cur_node.data
cur_idx+=1
def search(self,data):
cur_node=self.head
cur_idx=0
while cur_node!=None:
cur_node=cur_node.next
if cur_node!=None:
if cur_node.data==data:
return "Found at Index: "+str(cur_idx)
cur_idx+=1
return "Element Not Found"
def selectionSort(self):
currentpre=self.head
current=currentpre.next
while current!=None:
minimum=current
nextprev=current
beforesort=nextprev
nextinner=current.next
while nextinner!=None:
if minimum.data>nextinner.data:
beforesort=nextprev
minimum=nextinner
nextprev=nextinner
nextinner=nextinner.next
if current.next==minimum :
minimumNext=minimum.next
currentpre.next=minimum
minimum.next=current
current.next=minimumNext
current=minimum
elif current.next!=minimum and current!=minimum:
currentNext=current.next
minimumNext=minimum.next
currentpre.next=minimum
minimum.next=currentNext
beforesort.next=current
current.next=minimumNext
current=minimum
currentpre=current
current=current.next
def bubbleSort(self):
numElements=self.length()-1
while numElements>0:
curr_idx=0
curr_node=self.head.next
while curr_idx<numElements:
if curr_node.data>curr_node.next.data:
curr_node.data,curr_node.next.data=curr_node.next.data,curr_node.data
curr_node=curr_node.next
curr_idx+=1
numElements-=1
def insertionSort(self):
numElements=self.length()
curr_prev=self.head
curr_node=curr_prev.next
ll=LinkedList()
ll.addElement(21)
ll.addElement(2)
ll.addElement(1)
ll.addElement(11)
# ll.display()
ll.addElementAtPosition(21,2)
# ll.display()
ll.removeElementAtHead()
ll.removeElementAtTail()
# ll.display()
ll.addElementAtHead(12)
ll.addElementAtTail(212)
# ll.display()
ll.removeElementAtIndex(0)
ll.display()
print(ll.get(2))
print(ll.search(215))
ll.insertionSort()
ll.display()