forked from mejtfk/Algorithms-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked List.py
More file actions
58 lines (57 loc) · 1.37 KB
/
Linked List.py
File metadata and controls
58 lines (57 loc) · 1.37 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
#simple linked list append node at left , right and at speecific location of linked list
class node:
def __init__(self,data):
self.data=data
self.next=None
class link:
def __init__(self):
self.head=None
#append new node at left side of linked list
def left(self,data):
box = node(data)
box.next=self.head
self.head=box
#append new node at right side of linked list
def right(self,data):
box= node(data)
if self.head is None:
self.head=box
return
temp=self.head
while temp.next:
temp=temp.next
temp.next=box
#append at specific location
def specific(self,data,index):
if index==1:
box=node(data)
box.next=self.head
self.head=box
return
i=1
n=self.head
while i<index-1 and n is not None:
n=n.next
i +=1
if n is None:
print("index out of bounnd sorry...")
return
else:
box=node(data)
box.next=n.next
n.next=box
def plink(self):
temp=self.head
while temp:
print(temp.data),
temp=temp.next
l=link()
l.right(40)
l.left(5)
l.left(4)
l.left(2)
l.right(55)
l.right(100)
l.specific(1001,3)
l.specific(5999,1)
l.plink()