# [Tutor] Making Doubly Linked List with Less Lines of Code.

WolfRage wolfrage8765 at gmail.com
Wed Dec 24 22:35:06 CET 2014

```I wrote some code recently to make a linked list of Nodes for a 2d
graph, so it consists of rows and columns. Now I wanted to make the code
support being doubly linked, forwards and backwards.  The difficult part
of this is that the links are per row and per column. But the code I
think is overly bloated. I am currently working on reducing the
complexity of it. If any one has the time to look at it, if you have
ideas for how I can re-write it to be much smaller I would appreciate
the information. If you need more code let me know, but I tried to
condense it since this singular function is around 325 lines of code.
Thank you.

The function to make the linked list:

previous_row_node = None
previous_col0_node = None
previous_col1_node = None
previous_col2_node = None
previous_col3_node = None
previous_col4_node = None
previous_col5_node = None
previous_col6_node = None
previous_col7_node = None
current_node = None
self.tile_list = list()
for row in range(0, self.rows):
for col in range(0, self.cols):
current_node = self.get_instance_of_id(str(col) + ',' +
str(row))
self.tile_list.append(current_node)
if row == 0:
if col == 0:
self.row0 = current_node
self.col0 = current_node
previous_col0_node = current_node
elif col == 1:
self.col1 = current_node
previous_col1_node = current_node
elif col == 2:
self.col2 = current_node
previous_col2_node = current_node
elif col == 3:
self.col3 = current_node
previous_col3_node = current_node
elif col == 4:
self.col4 = current_node
previous_col4_node = current_node
elif col == 5:
self.col5 = current_node
previous_col5_node = current_node
elif col == 6:
self.col6 = current_node
previous_col6_node = current_node
elif col == 7:
self.col7 = current_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
current_node.prev_row_node = previous_row_node
previous_row_node = current_node
elif row == 1:
if col == 0:
self.row1 = current_node
previous_row_node = None
previous_col0_node.next_col_node = current_node
current_node.prev_col_node = previous_col0_node
previous_col0_node = current_node
elif col == 1:
previous_col1_node.next_col_node = current_node
current_node.prev_col_node = previous_col1_node
previous_col1_node = current_node
elif col == 2:
previous_col2_node.next_col_node = current_node
current_node.prev_col_node = previous_col2_node
previous_col2_node = current_node
elif col == 3:
previous_col3_node.next_col_node = current_node
current_node.prev_col_node = previous_col3_node
previous_col3_node = current_node
elif col == 4:
previous_col4_node.next_col_node = current_node
current_node.prev_col_node = previous_col4_node
previous_col4_node = current_node
elif col == 5:
previous_col5_node.next_col_node = current_node
current_node.prev_col_node = previous_col5_node
previous_col5_node = current_node
elif col == 6:
previous_col6_node.next_col_node = current_node
current_node.prev_col_node = previous_col6_node
previous_col6_node = current_node
elif col == 7:
previous_col7_node.next_col_node = current_node
current_node.prev_col_node = previous_col7_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
previous_row_node = current_node
elif row == 2:
if col == 0:
self.row2 = current_node
previous_row_node = None
previous_col0_node.next_col_node = current_node
previous_col0_node = current_node
elif col == 1:
previous_col1_node.next_col_node = current_node
previous_col1_node = current_node
elif col == 2:
previous_col2_node.next_col_node = current_node
previous_col2_node = current_node
elif col == 3:
previous_col3_node.next_col_node = current_node
previous_col3_node = current_node
elif col == 4:
previous_col4_node.next_col_node = current_node
previous_col4_node = current_node
elif col == 5:
previous_col5_node.next_col_node = current_node
previous_col5_node = current_node
elif col == 6:
previous_col6_node.next_col_node = current_node
previous_col6_node = current_node
elif col == 7:
previous_col7_node.next_col_node = current_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
previous_row_node = current_node
elif row == 3:
if col == 0:
self.row3 = current_node
previous_row_node = None
previous_col0_node.next_col_node = current_node
previous_col0_node = current_node
elif col == 1:
previous_col1_node.next_col_node = current_node
previous_col1_node = current_node
elif col == 2:
previous_col2_node.next_col_node = current_node
previous_col2_node = current_node
elif col == 3:
previous_col3_node.next_col_node = current_node
previous_col3_node = current_node
elif col == 4:
previous_col4_node.next_col_node = current_node
previous_col4_node = current_node
elif col == 5:
previous_col5_node.next_col_node = current_node
previous_col5_node = current_node
elif col == 6:
previous_col6_node.next_col_node = current_node
previous_col6_node = current_node
elif col == 7:
previous_col7_node.next_col_node = current_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
previous_row_node = current_node
elif row == 4:
if col == 0:
self.row4 = current_node
previous_row_node = None
previous_col0_node.next_col_node = current_node
previous_col0_node = current_node
elif col == 1:
previous_col1_node.next_col_node = current_node
previous_col1_node = current_node
elif col == 2:
previous_col2_node.next_col_node = current_node
previous_col2_node = current_node
elif col == 3:
previous_col3_node.next_col_node = current_node
previous_col3_node = current_node
elif col == 4:
previous_col4_node.next_col_node = current_node
previous_col4_node = current_node
elif col == 5:
previous_col5_node.next_col_node = current_node
previous_col5_node = current_node
elif col == 6:
previous_col6_node.next_col_node = current_node
previous_col6_node = current_node
elif col == 7:
previous_col7_node.next_col_node = current_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
previous_row_node = current_node
elif row == 5:
if col == 0:
self.row5 = current_node
previous_row_node = None
previous_col0_node.next_col_node = current_node
previous_col0_node = current_node
elif col == 1:
previous_col1_node.next_col_node = current_node
previous_col1_node = current_node
elif col == 2:
previous_col2_node.next_col_node = current_node
previous_col2_node = current_node
elif col == 3:
previous_col3_node.next_col_node = current_node
previous_col3_node = current_node
elif col == 4:
previous_col4_node.next_col_node = current_node
previous_col4_node = current_node
elif col == 5:
previous_col5_node.next_col_node = current_node
previous_col5_node = current_node
elif col == 6:
previous_col6_node.next_col_node = current_node
previous_col6_node = current_node
elif col == 7:
previous_col7_node.next_col_node = current_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
previous_row_node = current_node
elif row == 6:
if col == 0:
self.row6 = current_node
previous_row_node = None
previous_col0_node.next_col_node = current_node
previous_col0_node = current_node
elif col == 1:
previous_col1_node.next_col_node = current_node
previous_col1_node = current_node
elif col == 2:
previous_col2_node.next_col_node = current_node
previous_col2_node = current_node
elif col == 3:
previous_col3_node.next_col_node = current_node
previous_col3_node = current_node
elif col == 4:
previous_col4_node.next_col_node = current_node
previous_col4_node = current_node
elif col == 5:
previous_col5_node.next_col_node = current_node
previous_col5_node = current_node
elif col == 6:
previous_col6_node.next_col_node = current_node
previous_col6_node = current_node
elif col == 7:
previous_col7_node.next_col_node = current_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
previous_row_node = current_node
elif row == 7:
if col == 0:
self.row7 = current_node
previous_row_node = None
previous_col0_node.next_col_node = current_node
previous_col0_node = current_node
elif col == 1:
previous_col1_node.next_col_node = current_node
previous_col1_node = current_node
elif col == 2:
previous_col2_node.next_col_node = current_node
previous_col2_node = current_node
elif col == 3:
previous_col3_node.next_col_node = current_node
previous_col3_node = current_node
elif col == 4:
previous_col4_node.next_col_node = current_node
previous_col4_node = current_node
elif col == 5:
previous_col5_node.next_col_node = current_node
previous_col5_node = current_node
elif col == 6:
previous_col6_node.next_col_node = current_node
previous_col6_node = current_node
elif col == 7:
previous_col7_node.next_col_node = current_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
previous_row_node = current_node
elif row == 8:
if col == 0:
self.row8 = current_node
previous_row_node = None
previous_col0_node.next_col_node = current_node
previous_col0_node = current_node
elif col == 1:
previous_col1_node.next_col_node = current_node
previous_col1_node = current_node
elif col == 2:
previous_col2_node.next_col_node = current_node
previous_col2_node = current_node
elif col == 3:
previous_col3_node.next_col_node = current_node
previous_col3_node = current_node
elif col == 4:
previous_col4_node.next_col_node = current_node
previous_col4_node = current_node
elif col == 5:
previous_col5_node.next_col_node = current_node
previous_col5_node = current_node
elif col == 6:
previous_col6_node.next_col_node = current_node
previous_col6_node = current_node
elif col == 7:
previous_col7_node.next_col_node = current_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
previous_row_node = current_node
elif row == 9:
if col == 0:
self.row9 = current_node
previous_row_node = None
previous_col0_node.next_col_node = current_node
previous_col0_node = current_node
elif col == 1:
previous_col1_node.next_col_node = current_node
previous_col1_node = current_node
elif col == 2:
previous_col2_node.next_col_node = current_node
previous_col2_node = current_node
elif col == 3:
previous_col3_node.next_col_node = current_node
previous_col3_node = current_node
elif col == 4:
previous_col4_node.next_col_node = current_node
previous_col4_node = current_node
elif col == 5:
previous_col5_node.next_col_node = current_node
previous_col5_node = current_node
elif col == 6:
previous_col6_node.next_col_node = current_node
previous_col6_node = current_node
elif col == 7:
previous_col7_node.next_col_node = current_node
previous_col7_node = current_node
if previous_row_node is not None:
previous_row_node.next_row_node = current_node
previous_row_node = current_node
```