forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_new_doublyLL.py
More file actions
68 lines (48 loc) · 1.16 KB
/
insert_new_doublyLL.py
File metadata and controls
68 lines (48 loc) · 1.16 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
class Node:
def __init__(Node, data):
Node.data = data
Node.next = None
Node.prev = None
def insertNodestart(head, newNode):
newNode.next = head
head.prev = newNode
newNode.prev=None
head=newNode
return head
def insertmiddle(head, newNode):
temp = head
while (temp.data != 'c'):
temp = temp.next
buffer = temp.next
temp.next = newNode
newNode.prev = temp
newNode.next = buffer
buffer.prev = newNode
return head
def insertend(head, newNode):
temp = head
while (temp.next != None):
temp = temp.next
temp.next = newNode
newNode.prev = temp
newNode.next = None
return head
def printList(head):
temp = head
while (temp):
print(temp.data)
temp = temp.next
head = Node('a')
nodeB = Node('b')
nodeC = Node('c')
nodeD = Node('d')
head.next = nodeB
nodeB.next = nodeC
nodeC.next = nodeD
nodeB.prev=head
nodeC.prev=nodeB
nodeD.prev=nodeC
# head=insertNodestart(head,Node('z'))
# head=insertmiddle(head,Node('z'))
head = insertend(head, Node('z'))
printList(head)