-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doubly Linked List.py
76 lines (56 loc) · 1.67 KB
/
Doubly 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
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 7 20:10:10 2020
@author: $parsh
"""
class DoubleNode:
def __init__(self, value=None):
self.value = value
self.radd = None
self.ladd = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def append(self, value) :
if self.head is None:
new_node = DoubleNode(value)
self.head = new_node
return
else :
new_node = DoubleNode(value)
cur_node = self.head
while cur_node.radd :
cur_node = cur_node.radd
cur_node.radd = new_node
new_node.ladd = cur_node
new_node.radd = None
def prepend(self,value) :
if self.head is None:
new_node = DoubleNode(value)
self.head = new_node
return
else :
new_node = DoubleNode(value)
cur_node = self.head
cur_node.ladd = new_node
new_node.radd = cur_node
self.head = new_node
new_node.ladd = None
def display(self):
elems = []
cur_node = self.head
while cur_node :
elems.append(cur_node.value)
cur_node = cur_node.radd
print(elems)
mylist = DoublyLinkedList()
mylist.prepend(1)
mylist.prepend(2)
mylist.prepend(3)
mylist.append(4)
mylist.append(1)
mylist.append(2)
mylist.append(3)
mylist.append(4)
print(mylist)
mylist.display()