Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

* Say I have the node class: */ public class GridNode { private GridNode right;

ID: 3808601 • Letter: #

Question

* Say I have the node class:

            */

            public class GridNode

{

                           private GridNode right;

                           private int data;

                           private GridNode down;

                           public GridNode(GridNode down, int data, GridNode right) {

                               this.setDown(down);

                               this.setData(data);

                               this.setRight(right);

                           }

                           public int getData() {

                               return data;

                           }

                           public void setData(int data) {

                               this.data = data;                                                                                                  

                           }

                           public GridNode getRight() {

                               return right;

                           }

                           public void setRight(GridNode right) {

                               this.right = right;

                           }

                           public GridNode getDown() {

                               return down;

                           }

                           public void setDown(GridNode down) {

                               this.down = down;

                           }

}}

            /*

Then I have another class called "Grid" whose constructor will link these nodes like a grid.

            Note: The node class has two pointers, a RIGHT and DOWN.

            The RIGHT pointers will link the nodes to the right of it to make a row and

            the DOWN to make the columns.

Also, make the final node created point to the first node in the row/col to make it circular.

            THE PROBLEM:

Initialize the grid structure mentioned in the "Grid" constructor with parameters (6,10).

Also a method that lets you insert a whole row or column to the grid after its made and a delete method.

            */

Diagram of the constructor:

            public class Grid

{

           

            GridNode head;

                       

            public Grid(int row, int col)

            {

                        //set up a 6 by 10

            }

            public void insert(){

            }

            Public void delete(){

}

}

Explanation / Answer

public class Grid {
  
class GridNode
{
private GridNode right;
private int data;
private GridNode down;
  
public GridNode(GridNode down, int data, GridNode right) {
this.setDown(down);
this.setData(data);
this.setRight(right);
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public GridNode getRight() {
return right;
}
public void setRight(GridNode right) {
this.right = right;
}
public GridNode getDown() {
return down;
}
public void setDown(GridNode down) {
this.down = down;
}
}
GridNode head;

public GridChegg(int row, int col)
{
GridNode[] headArray = new GridNode[col];// Create an array to store First row . SO that we can set down pointer of last row to first row
for(int i=0; i< row; i++)
{
GridNode temp = null, runner = null;
for(int j=0; j< col; j++)
{
if(temp == null)
{
temp = new GridNode(null, j+1, null);
if(i != 0)
{
temp.setDown(headArray[j].getDown());
headArray[j].setDown(temp);
}
if(i == 0)
{
temp.setDown(temp);
}
headArray[j] = temp;// store every node so that we can keep track of one level above
  
if(head == null)
head = temp; // set global head for the first time
}
else
{
runner = new GridNode(null, j+1, null);
if(i != 0)
{
runner.setDown(headArray[j].getDown());
headArray[j].setDown(runner);
}
if(i == 0)
{
runner.setDown(runner);
}
temp.setRight(runner);
temp = temp.getRight();
headArray[j] = temp;
}
}
headArray[col-1].setRight(headArray[0]);// here we are setting last row node to first row node
}
}
  
// This method inserts row at the end, we can always customise it to insert row anywhere.
public void insert()
{
GridNode dummyHead = null, origDummyHead = null;
dummyHead = head;
while(dummyHead.getDown() != head)
dummyHead = dummyHead.getDown();// go till the last RowHead
  
origDummyHead = dummyHead;
  
GridNode rowHead = null, temp = null, runner = null;
while(dummyHead.getRight() != origDummyHead)
{
if(rowHead == null)
{
rowHead = new GridNode(null, 5, null);
rowHead.setDown(dummyHead.getDown());
dummyHead.setDown(rowHead);
temp= rowHead;
}
else
{
runner = new GridNode(null, 5, null);
temp.setRight(runner);
temp = temp.getRight();
temp.setDown(dummyHead.getDown());
dummyHead.setDown(temp);
}
dummyHead = dummyHead.getRight();
}
temp.setRight(rowHead);
}
  
// This method deletes Last row, but we can always customise it to delete any row
public void delete()
{
GridNode dummyHead = null, headRunner= null;
dummyHead = head;
headRunner = head; // this pointer helps us with second last rows set down.
  
while(dummyHead.getDown().getDown() != head) // here we are reaching till second last row
dummyHead = dummyHead.getDown();
  
GridNode origDummyHead = null;
origDummyHead = dummyHead;
  
while(dummyHead.getRight() != origDummyHead)
{
dummyHead.setDown(headRunner);
headRunner = headRunner.getRight();
dummyHead = dummyHead.getRight();
}
}


public static void main(String[] args) {
  
Grid gc = new Grid(6, 10);
System.out.println("Grid created Succesfully");
}
  
}