Skip to content

Commit 95da8b7

Browse files
author
Bhrigu Kansra
authored
Merge pull request #34 from Pratyush2703/doubly-linked-list
Doubly linked list
2 parents b3d9dac + 1253618 commit 95da8b7

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#Defined Node
2+
class ListNode:
3+
def __init__(self, data):
4+
"constructor class to initiate this object"
5+
6+
# store data
7+
self.data = data
8+
9+
# store reference (next item)
10+
self.next = None
11+
12+
# store reference (previous item)
13+
self.previous = None
14+
return
15+
16+
def has_value(self, value):
17+
"method to compare the value with the node data"
18+
if self.data == value:
19+
return True
20+
else:
21+
return False
22+
23+
#Doubly Linked List
24+
25+
class DoubleLinkedList:
26+
def __init__(self):
27+
"constructor to initiate this object"
28+
29+
self.head = None
30+
self.tail = None
31+
return
32+
33+
def list_length(self):
34+
"returns the number of list items"
35+
36+
count = 0
37+
current_node = self.head
38+
39+
while current_node is not None:
40+
# increase counter by one
41+
count = count + 1
42+
43+
# jump to the linked node
44+
current_node = current_node.next
45+
46+
return count
47+
48+
def output_list(self):
49+
"outputs the list (the value of the node, actually)"
50+
current_node = self.head
51+
52+
while current_node is not None:
53+
print(current_node.data)
54+
55+
56+
current_node = current_node.next
57+
58+
return
59+
60+
def unordered_search (self, value):
61+
"search the linked list for the node that has this value"
62+
63+
64+
current_node = self.head
65+
66+
67+
node_id = 1
68+
69+
70+
results = []
71+
72+
while current_node is not None:
73+
if current_node.has_value(value):
74+
results.append(node_id)
75+
76+
77+
current_node = current_node.next
78+
node_id = node_id + 1
79+
80+
return results
81+
82+
#Adding nodes
83+
84+
def add_list_item(self, item):
85+
"add an item at the end of the list"
86+
87+
if isinstance(item, ListNode):
88+
if self.head is None:
89+
self.head = item
90+
item.previous = None
91+
item.next = None
92+
self.tail = item
93+
else:
94+
self.tail.next = item
95+
item.previous = self.tail
96+
self.tail = item
97+
98+
return
99+
100+
#Removing Nodes
101+
102+
def remove_list_item_by_id(self, item_id):
103+
"remove the list item with the item id"
104+
105+
current_id = 1
106+
current_node = self.head
107+
108+
while current_node is not None:
109+
previous_node = current_node.previous
110+
next_node = current_node.next
111+
112+
if current_id == item_id:
113+
# if this is the first node (head)
114+
if previous_node is not None:
115+
previous_node.next = next_node
116+
if next_node is not None:
117+
next_node.previous = previous_node
118+
else:
119+
self.head = next_node
120+
if next_node is not None:
121+
next_node.previous = None
122+
# we don't have to look any further
123+
return
124+
125+
# needed for the next iteration
126+
current_node = next_node
127+
current_id = current_id + 1
128+
129+
return
130+
131+

0 commit comments

Comments
 (0)