使用for循环创建一个双向链表java

时间:2021-10-24 20:34:41

I want to create n elements in a doubly linked list in Java. I try to build a matrix with right, left, up and down references to build a pentomino game which is an exact cover problem, to solve it. Therefore I need to implement the Algorithm X from Knuth.

我想在Java中的双向链表中创建n个元素。我尝试构建一个带有右,左,上和下参考的矩阵来构建一个五角形游戏,这是一个确切的覆盖问题,以解决它。因此,我需要从Knuth实现算法X.

How do I create n-elements in a doubly linked list with a for-loop? I don't want to lose my h-Node, because it is the entry into my double linked list.

如何使用for循环在双向链表中创建n元素?我不想丢失我的h-Node,因为它是我的双链表的入口。

My Code:

public class PentominoWLIDLX
{
  static Node h;                  // header element

  public PentominoWLIDLX(int n)
  {
    h = new Node();
    // create n columns
    Node temp = h;
    for (int i = 1; i <= n; i++)
    {
        Node newColumn = new Node(i);

        temp.R = newColumn.L;

        temp.L = newColumn.R;

        temp = newColumn;
    }
  }


class Node // represents 1 element or header
{
    Node C;    // reference to column-header << h?,
    Node L;    // left
    Node R;    // right
    Node U;    // reference up
    Node D;    // down reference down

    int position;

    Node()
    {
        C = L = R = U = D = this;
    }

    Node(int i)
    {
        C = L = R = U = D = this;  // double-linked circular list
        this.position = i;
    }

    public int getPosition()
    {
        return this.position;
    }
}   // end of class

public static void main(String[] args)
{
    PentominoWLIDLX p = new PentominoWLIDLX(3);
    System.out.println("h. " + h.getPosition());
    System.out.println("h.getClass: " + h.getClass());
    System.out.println("h.1R: " + h.R.getPosition());
    System.out.println("h.2R: " + h.R.R.getPosition());
    System.out.println("h.3R: " + h.R.R.R.getPosition());
    System.out.println("h.4R: " + h.R.R.R.R.getPosition());
    System.out.println("h.1L: " + h.L.getPosition());
    System.out.println("h.2L: " + h.L.L.getPosition());
    System.out.println("h.3L: " + h.L.L.L.getPosition());
    System.out.println("h.4L: " + h.L.L.L.L.getPosition());
    System.out.println("h.U " + h.U.getPosition());
}
}//end of class

1 个解决方案

#1


Solved:

public PentominoWLIDLX(int n)
{
    h = new Node();
    columnList = new ArrayList<PentominoWLIDLX.Node>();
    rowList = new ArrayList<PentominoWLIDLX.Node>();
    // create n columns
    columnList.add(h);

    for (int i = 1; i <= n; i++)
    {
        Node column = new Node(i);  // create new column
        columnList.add(column);     // add it to list
    }

    columnList.get(0).L = columnList.get(columnList.size()-1);
    columnList.get(columnList.size()-1).R = columnList.get(0);
    for (int i = 0; i < columnList.size(); i++)
    {
        if (i > 0)
        columnList.get(i).L = columnList.get(i-1);

        if (i < (columnList.size()-1))
        columnList.get(i).R = columnList.get(i+1);
    }
}

#1


Solved:

public PentominoWLIDLX(int n)
{
    h = new Node();
    columnList = new ArrayList<PentominoWLIDLX.Node>();
    rowList = new ArrayList<PentominoWLIDLX.Node>();
    // create n columns
    columnList.add(h);

    for (int i = 1; i <= n; i++)
    {
        Node column = new Node(i);  // create new column
        columnList.add(column);     // add it to list
    }

    columnList.get(0).L = columnList.get(columnList.size()-1);
    columnList.get(columnList.size()-1).R = columnList.get(0);
    for (int i = 0; i < columnList.size(); i++)
    {
        if (i > 0)
        columnList.get(i).L = columnList.get(i-1);

        if (i < (columnList.size()-1))
        columnList.get(i).R = columnList.get(i+1);
    }
}