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

Danny Yoo dyoo at hashcollision.org
Tue Dec 30 20:37:13 CET 2014

>> What are the _operations_ you want to support?  Can you say more about
>> this?
> I need to support lookups, no insertions or deletions are required. This
> code will be used to quickly lookup nodes, and check for specific patterns
> by checking the nodes values in either a row or column.

As it stands, I believe you want these operations:

    * Construct a game grid with a certain number of rows and columns
    * Find a tile within the grid, given its row and column.
    * Given a tile in the grid, find the neighboring tiles around it.

If that's the case, then none of this requires linked list storage.

Instead, we can represent this as a list of rows.  Each row element
would be itself a list of tiles.  In short, a matrix.  See:

Its construction wouldn't be too bad.  Something like:

    self.rows = [[None] * self.num_of_cols for i in range(self.num_of_rows)]

Finding a tile (i, j) in such a structure would be direct list indexing.


And finding neighboring tiles also wouldn't be too bad: Given a tile
at (i, j), we can find its neighbors through arithmetic (i plus or
minus one, j plus or minus one).

If the game grid were a lot more dynamic and non-squarish, then that
might make it appropriate.  Linked lists give us a flexible way to
represent such a game board.  But at the moment, I think it's overkill
for the scenario you're showing us.

Good luck!

More information about the Tutor mailing list