-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.py
52 lines (52 loc) · 1.42 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
class LinkedNode:
def __init__(self,d=0,n=None):
self.data=d
self.next=n
class LinkedList(LinkedNode):
def __init__(self):
self.front=None
def get(self,ind):
i=0
cur=self.front
while i<ind:
cur=cur.next
i=i+1
return cur.data
def set(self,ind,value):
i=0
cur=self.front
while i<ind:
cur=cur.next
i=i+1
cur.data=value
def __str__(self):
list1=[]
current=self.front
while current!=None:
list1.append(current.data)
current=current.next
return 'Your linkedlist: [%s]' % ', '.join(map(str, list1))
def add(self,value):
if self.front == None:
self.front=LinkedNode(value)
else:
current=self.front
while current.next!=None:
current=current.next
current.next= LinkedNode(value)
def insert(self,ind,value):
if ind==0:
self.front=LinkedNode(value,self.front)
else:
cur=self.front
for i in range(0,ind-1):
cur=cur.next
cur.next=LinkedNode(value,cur.next)
def removeind(self,ind):
if ind==0:
self.front=self.front.next
else:
cur=self.front
for i in range(0,ind-1):
cur=cur.next
cur.next=cur.next.next