基础算法系列 之单链表

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素,每个结点的构成:元素 + 指针。其代码如下:

public class Node{
    
    
	int data;
	Node next;
	public Node(int data){
    
    
		this.data=data;
	}
	public void *append*(Node node){
    
    	//追加节点
		Node currentNode=this;
		while(true){
    
    
			Node cnode=currentNode.next;
			currentNode=cnode;
			if(cnode==null){
    
    
				break;
			}
		}
		cnode.next=node;
		return this;
	}
	public Node next(){
    
    
		return this.next;
	}
	public int getData(){
    
    
		return this.data;
	}
	public boolean isLast(){
    
    
		return next==null;
	}
	public void removeNext(){
    
    
		Node nextnew=next.next;
		this.next=nextNew;
	}
	public void show(){
    
    
		Node currentNode=this;
		while(true){
    
    
			System.out.println(currentNode.data);
			currentNode=currentNode.next;
			if(currentNode==null){
    
    
				break;
			}
		}
	}
	public void after(Node node){
    
    	//插入节点
		Node nextNext=next;
		this.next=node;
		node.next=nextNext;
	}
}
 

猜你喜欢

转载自blog.csdn.net/langxiaolin/article/details/113460712