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

我想在Java中的双向链表中创建n个元素。我尝试用右,左,上和下引用构建一个矩阵,以构建一个精确覆盖问题的pentomino游戏来解决它。因此我需要从Knuth实现算法X.

如何使用for-loop在双向链表中创建n元素?

我不想失去我的h-Node,因为它是进入我的双链表。

我的代码:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值