Skip to content

Commit ba8011f

Browse files
authored
Update DLLfullsamplecode.py
1 parent 71531a1 commit ba8011f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Doublylinkedlist/DLLfullsamplecode.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
11

2+
class Node:
3+
#constructor to create a new node
4+
def __init__(self,data):
5+
self.data=data
6+
self.next=None
7+
self.prev=None
8+
class dll:
9+
#constructor for empty doublylinkedlist
10+
def __init__(self):
11+
self.head=None
12+
#function for reverse a doubly linked list
13+
def reverse():
14+
temp=None
15+
curr=head
16+
#swap next and prev for all nodes of doubly linked list
17+
while temp is not None:
18+
temp=curr.prev
19+
curr.prev=curr.next
20+
curr.next=prev
21+
curr=curr.prev
22+
#before changing cases check for cases
23+
if temp is not None:
24+
head=temp.prev
25+
def push(data):
26+
new_node=Node(data)
27+
new_node.next=head
28+
if head is not None:
29+
head.prev=new_node
30+
head=new_node
31+
def printlist(node):
32+
while(node is not None):
33+
print node.data
34+
node=node.next
35+
#Driver program tot test the above function
36+
dll=DoublylinkedList()
37+
dll.push(2)
38+
dll.push(5)
39+
dll.push(7)
40+
dll.push(11)
41+
dll.push(6)
42+
print "\n Linked lsit"
43+
dll.printList(dll.head)
44+
#reverse doubly linked list
45+
dll.reverse()
46+
print "\n Reversed Linked list"
47+
dll.printList(dll.head)
48+
ss

0 commit comments

Comments
 (0)