-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathDouble_Linked_List.py
95 lines (95 loc) · 2.12 KB
/
Double_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
###IMPLEMENTATION OF DOUBLE LINKED LIST##
class Node:
def __init__(self,dataval=None):
self.dataval=dataval
self.next=None
self.prev=None
class DoubleList:##List Operations
def __init__(self):
self.head=None
def display(self):##Display the list
curr=self.head
while curr is not None:
print(curr.dataval)
curr=curr.next
def pushFront(self,item):
NewNode=Node(item)
NewNode.next=self.head
NewNode.prev=None
self.head=NewNode
return
def pushLast(self,item):
NewNode=Node(item)
curr=self.head
while curr.next is not None:
curr=curr.next
curr.next=NewNode
NewNode.prev=curr
NewNode.next=None
return
def pushMiddle(self,item,pos):
NewNode=Node(item)
curr=self.head
count=1
while count<pos-1:
curr=curr.next
count=count+1
NewNode.next = curr.next.next
curr.next=NewNode
NewNode.prev=curr
return
def deleteFront(self):
curr=self.head
temp=curr.next
temp.prev=None
self.head=temp
curr=None
return
def deleteLast(self):
curr=self.head
while curr.next.next is not None:
curr=curr.next
temp=curr.next
curr.next=None
temp=None
return
def deleteMiddle(self,pos):
curr=self.head
count=1
while count<pos-1:
curr=curr.next
count=count+1
temp=curr.next
curr.next=curr.next.next
temp.prev=curr.next.prev
temp=None
return
list1=DoubleList()
list1.head=Node("Mon")
e2=Node("Tue")
e3=Node("Wed")
list1.head.prev=None
list1.head.next=e2
e2.prev=list1.head.next
e2.next=e3
e3.prev=e2.next
list1.display()
print()
list1.pushFront("Front")
list1.display()
print()
list1.pushLast("Last")
list1.display()
print()
list1.pushMiddle("Three",3)
list1.display()
print()
list1.deleteFront()
list1.display()
print()
list1.deleteLast()
list1.display()
print()
list1.deleteMiddle(2)
list1.display()
print()