java 双链表,没有循环

就是那样写就好了,写清楚点

今天才发现我是个智障Orz

package DoubleLink;

public class DoubleLink {
	public Node pointer;
	public Node head;
	public Node tail;
	public int size;
	public DoubleLink()
	{
	 	head=new Node();
	 	tail=head;
	 	tail.root=head;
	 	head.next=tail;
	 	size=0;
	 	
	}
	public void AddNode(int i)
	{
		Node node=new Node(i);
	  tail.LinkNext(node);
	  node.UpRoot(tail);
	  tail=node;
	  size=size+1;
	}
	public Node getNode(int index)
	{
     pointer=tail;
     int current=0;
     while(current!=size-index)
     {
    	 pointer=pointer.root();
    	 current++;
     }
     return pointer;
	}
	public void printLink()
	{
		pointer=head.next();
		while(pointer!=tail)
		{
			System.out.print(pointer.getValue()+" ");
			pointer=pointer.next;
		}
		System.out.println(tail.getValue());
	}
}
class Node
{
	Node next,root;
	int value;
	public Node(){}
	public Node(int value)
	{
		this.value=value;
	}
	public Node next()
	{
		return this.next;
	}
	public int getValue()
	{
		return this.value;
	}
	public void LinkNext(Node node)
	{
		this.next=node;
	}
	public Node root()
	{
		return this.root;
	}
	public void UpRoot(Node node)
	{
		this.root=node;
	}
}

package DoubleLink;

public class LinkTest {
public static void main(String[] args)
{
	DoubleLink link=new DoubleLink();
	Node node=new Node();
	for(int i=0;i<10;i++)
	{
		
		link.AddNode(i);	
	}
	link.printLink();
	System.out.println(link.getNode(3).getValue());
	
}
}
运行结果:
0 1 2 3 4 5 6 7 8 9
2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值