-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublelink.py
64 lines (50 loc) · 1.3 KB
/
doublelink.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
class Node:
def __init__(self,data):
self.data=data
self.prev=None
self.next=None
class LinkedList:
def __init__(self):
self.head=None
def printlist(self):
printval=self.head
while printval is not None:
print(printval.data, end="")
printval=printval.next
print()
def traverse(self):
ptr=self.head
while ptr.next is not None:
ptr=ptr.next
while ptr.prev is not None:
print(ptr.data,end="")
ptr=ptr.prev
print(ptr.data)
def insert(self):
new=Node(55)
loc=int(input("Enter location"))
ptr=self.head
while ptr.data!=loc:
ptr=ptr.next
new.next=ptr.next
new.next.prev=new
ptr.next=new
new.prev=ptr
if __name__== "__main__":
llist=LinkedList()
First=Node(1)
llist.head=First
second=Node(2)
third=Node(3)
fourth=Node(4)
llist.head.next=second
second.next=third
third.next=fourth
fourth.next=None
fourth.prev=third
third.prev=second
second.prev=First
First.prev=None
llist.printlist()
llist.traverse()
llist.insert()