JAVA: Linked list problem I have a node class say: *****************************
ID: 3801383 • Letter: J
Question
JAVA: Linked list problem
I have a node class say:
************************************************
private static class Node {
private int data;
private Node right;
private Node down;
public Node(Node down, int data, Node right) {
this.down = down;
this.data = data;
this.right = right;
}
}
****************************************************************
I need to make a grid class whose constructor needs to links the rows and columns of the Nodes of for example a 5x4 grid.
so
public class Grid{
private Node head;
public Grid(){
//at (0,0), it would connect to all the nodes below it and all the nodes to the right of it. and so on and so on
//I'd also note it should be a circularly linked list.
}
}
anyhelp settting up that constructor would be appreciated. Lmk if you need more info
Explanation / Answer
class Node { Node next; int num; public Node(int val) { num = val; next = null; } } public class LinkedList { Node head; public LinkedList(int val) { head = new Node(val); } public void append(int val) { Node tmpNode = head; while (tmpNode.next != null) { tmpNode = tmpNode.next; } tmpNode.next = new Node(val); } public void insert(int val) { Node currentNode = head; Node nextNode = head.next; if (currentNode.num > val) { Node tmpNode = head; head = new Node(val); head.next = tmpNode; return; } if (nextNode != null && nextNode.num > val) { currentNode.next = new Node(val); currentNode.next.next = nextNode; return; } while (nextNode != null && nextNode.numRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.