I would like to create a doubly linked list without using the Java linked list object. I want to see if I can do so with a two dimensional array in which:
Here is what I'd like to display but not sure if I'm doing it correctly
Here's my code, any help is greatly appreciated.
public class TwoDArrayLinkedList {
static class Node {
Node forwardPointer;
Node backwardPointer;
int data;
}
static void display(Node head) {
Node RightPointer;
Node LeftPointer = head;
while (LeftPointer != null) {
RightPointer = LeftPointer;
while (RightPointer != null) {
System.out.print(RightPointer.data + " ");
RightPointer = RightPointer.forwardPointer;
}
System.out.println();
System.out.println();
System.out.println();
LeftPointer = LeftPointer.backwardPointer;
}
}
static Node construct(int array[][], int i, int j, int m, int n) {
if (i > n - 1 || j > m - 1)
return null;
Node tempNode = new Node();
tempNode.data = array[i][j];
tempNode.forwardPointer = construct(array, i, j + 1, m, n);
tempNode.backwardPointer = construct(array, i + 1, j, m, n);
return tempNode;
}
public static void main(String args[]) {
int array[][] = { { 12, 22, 33 },
{ 44, 54, 61 },
{ 72, 82, 93 } };
int m = 3, n = 3;
Node head = construct(array, 0, 0, m, n);
display(head);
}
}
you aren't making it doubly-linked list you have written code for a linked list matrix such that each node is connected to its next right and down node.
to make 2D array as a doubly linked list you need to traverse each 2D Array element and needs to create a new node for each value and assign forward and backward reference. this might help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.