Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-Dimensional Quad-Directional Linked List

Ok.. so I decided to try linked lists in Java rather than the usual C++ which I'm used to references an pointers in..

Traversal can be Up, Down, Left, Right for a center node. For nodes in the corners, they only move two directions and nodes on the edges can move 3 directions.. All others can move 4.

Problem:

When my program leaves the constructor, my nodes are deleted somehow :S When I use the get/set, it cannot traverse the links because they are null.. Except for the first node.

My node is:

package linkedlist;

public class Node {
    public Node Up, Down, Left, Right;
    public int Value;

    public Node() {
        Value = -1;
        Up = Down = Left = Right = null;
    }
}

I do implementation like:

package linkedlist;

public class Matrix {
    private int Width, Height;
    private Node Reference;

    public Matrix(int Width, int Height) {
        Reference = new Node();
        this.Width = Width; this.Height = Height;
        Node RowIterator = Reference, ColumnIterator = Reference;

        for (int I = 0; I < Height; ++I) {
            for (int J = 0; J < Width; ++J) {
                if (I == 0) {
                    if (J < Width - 1) {
                        RowIterator.Right = new Node();
                        RowIterator.Right.Left = RowIterator;
                        RowIterator = RowIterator.Right;
                    }
                }
                else {
                    if (I < Height - 1) {
                        ColumnIterator.Down = new Node();
                    }

                    RowIterator = ColumnIterator;
                    RowIterator.Right = new Node();
                    RowIterator.Up = ColumnIterator;
                    RowIterator.Up.Down = RowIterator;
                    RowIterator.Right.Left = RowIterator;
                    RowIterator.Right.Up = RowIterator.Up.Right;
                    RowIterator = RowIterator.Right;

                    ColumnIterator = ColumnIterator.Down;
                }
            }
        }
    }

    public void SetValue(int I, int J, int Value) {
          //Same as get except it sets rather than returns..
    }

    public int GetValue(int I, int J) {
        RowIterator = ColumnIterator = Reference;
        for (int K = 0; K < J; ++K) {
            for (int L = 0; L < I; ++L) {
                RowIterator = RowIterator.Right;
            }

            ColumnIterator = ColumnIterator.Down;
            RowIterator = ColumnIterator;
        }

        return RowIterator.Value;
    }
}

And the main like:

package linkedlist;

public class LinkedList {
    public static void main(String[] args) {
        Matrix M = new Matrix(6, 6);

        M.SetValue(3, 3, 10);
    }
}

So when I attempt to set the value at near middle of the matrix, it throws a null pointer error.. If I attempt to set it in the constructor, it works just fine.. Thus my nodes must be somehow getting garbage cleaned..

like image 751
Brandon Avatar asked Oct 06 '22 08:10

Brandon


1 Answers

You iterate across the row as many times as you iterate down the column. I.e., in your example, the actual node that is accessed is (9, 3), out of the bounds of your Matrix.

Instead, you should iterate once across the row, and then once down the column.

for (int K = 0; K < J; ++K) {
    Iterator = Iterator.Down;
}

for (int L = 0; L < I; ++L) {
    Iterator = Iterator.Right;
}

Any particular reason you aren't just using a 2-dimensional array?

like image 56
Alex DiCarlo Avatar answered Oct 26 '22 08:10

Alex DiCarlo