-
Notifications
You must be signed in to change notification settings - Fork 1
/
Doublelinkedlist.py
46 lines (40 loc) · 1.04 KB
/
Doublelinkedlist.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
#code
class Node:
def __init__(self,data):
self.data = data
self.next = None
self.prev = None
class Linkedlist:
def __init__(self):
self.head = None
self.tail = None
def insert(self,data):
new_node = Node(data)
if self.head == None and self.tail == None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
new_node.prev = self.tail
self.tail = new_node
def display(self):
if self.head == None:
return
else:
temp = self.head
while temp:
print(temp.data,end=" ")
temp = temp.next
print()
temp1= self.tail
while temp1:
print(temp1.data,end=" ")
temp1 = temp1.prev
l = Linkedlist()
l.insert(10)
l.insert(1)
l.insert(5)
l.insert(20)
l.insert(25)
l.insert(30)
l.display()