java单链表的基本操作

package linklist;

public class LinkList {
	private Node head = null;// 链表头节点
	private Node tail = null;// 链表尾节点
	private Node pointer = null;// 遍历链表的节点

	public LinkList(Node node) {
		head=new Node();
		head.LinkNext(node);
		tail = node;
	}
	// 在链表末尾添加节点
	public void AddNode(Node node) {
		tail.LinkNext(node);
		tail = tail.next();
	}
	// 返回链表长度
	public int getLength() {
		pointer=head.next();
		int current=0;
		while(pointer!=tail)
		{
		 current++;
		 pointer=pointer.next();
		}
		return ++current;  //注意++一定要写前面,被还惨了QAQ
	}
	// 打印链表所有的元素
	public void printLink() {
		pointer=head.next();
		while(pointer!=tail)
		{
		System.out.print(pointer.getValue()+" ");
		pointer=pointer.next();
		}
		System.out.println(tail.getValue());
		
	
	}
	// 在链表指定位置插入节点
	public void insertAt(int index, Node node) {
	  if(index>this.getLength())
	  {
		  System.out.print("wrong");
	  }
	  else
	  {
		pointer=head;
		for(int i=0;i<index-1;i++)
			pointer=pointer.next();
	  /*int current=0;
	  while(current!=index)
	  {
		 pointer=pointer.next();
		 current++;
	  }*/
	node.LinkNext(pointer.next());
	pointer.LinkNext(node);
	}
	}
	// 在链表指定位置删除节点
	public void removeAt(int index) {
		if(index>this.getLength())
		{
			System.out.print("wrong");
		}
		else
		{
		pointer=head;
		 Node q=pointer;
		 for(int i=0;i<index;i++)
		 {
			 q=pointer;
			 pointer=pointer.next();
		 }
		 q.LinkNext(pointer.next());
		 pointer.LinkNext(null);
		}
		 /* int current=0;
		  while(current!=index)
		  {
			 pointer=pointer.next();
			 current++;
		  }
	   pointer.LinkNext(pointer.next());
	   */
	
}
}

class Node {
	private Object value;// 值
	private Node next;// 下一个节点
    public Node(){}
	public Node(Object value) {
		this.value = value;
	}
      // 获取下一个节点
	public Node next() {
		return this.next;
	}
      // 链接下一个节点
	public void LinkNext(Node next) {
		this.next = next;
	}
      // 设置当前节点的值
	public void setValue(Object value) {
		this.value = value;
	}
      // 获取当前节点的值
	public Object getValue() {
		return this.value;
	}
}

package linklist;

public class testlink {
	public static void main(String[] args) {
		Node n1=new Node("a");
		Node n2=new Node("b");
	    Node n3=new Node("c");
	    Node n4=new Node("d");
	    Node n5=new Node("e");
	    
	    LinkList list=new LinkList(n1);
	    list.AddNode(n2);
	    list.AddNode(n3);
	    list.AddNode(n4);
	    
	    list.printLink();//此时输出:a	b	c	d
	    System.out.println(list.getLength());//此时输出:4
	    
	    list.insertAt(4, n5);
	    //System.out.println(list.getLength());
	    list.printLink();//此时输出:a	b	c	e	d
	    list.removeAt(2);
	    list.printLink();//此时输出:a	c	e	d
	    System.out.println(list.getLength());

	} 

}


补充内容:

单链表节点Node
比如说有A,B,C,D四个Node对象,它们按顺序一个接一个
next指的是下一个
A.next=B;
B.next=C;
C.next=D;

root指的是上一个
B.root=A;
C.root=B;
D.root=C;
来源:http://zhidao.baidu.com/link?url=SugMe6R5e-4KV15Pkmo7tzjqLHn8q2zJGUqpU5QSnvGzCnsz2_gb5TC0FXf74SWJMk6yA6tP84ib33H8vhFn0K

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值