单链表的具体实现

  

     单链表是链表中结构最简单的。一个单链表的节点(Node)分为两个部分,第一个部分(data)保存或者显示关于节点的信息,另一个部分存储下一个节点的地址。最后一个节点存储地址的部分指向空值。

  单向链表只可向一个方向遍历,一般查找一个节点的时候需要从第一个节点开始每次访问下一个节点,一直访问到需要的位置。而插入一个节点,对于单向链表,我们只提供在链表头插入,只需要将当前插入的节点设置为头节点,next指向原头节点即可。删除一个节点,我们将该节点的上一个节点的next指向该节点的下一个节点。

单链表的具体实现:


public class SingleLinkedList {
	
	//链表节点类
	private class Node{
		private Object data ; //每个节点数据
		private Node next ; //每个节点指向下一个节点的连接
		
		public Node(Object data){
			this.data=data ;
		}
	}
	
	private int size ; //链表节点个数
	private Node head ; //头节点
	
	public SingleLinkedList(){
		size = 0 ;
		head = null ;
	}
	
	//在链表头添加元素
	public Object addHead(Object obj){
		Node newHead = new Node(obj) ;
		if(size==0){
			head=newHead;
		}else{
			newHead.next = head ;
			head = newHead;
		}
		size++;
		return obj ;
	}

	//在链表头删除元素
	public Object deleteHead(){
		Object obj = head.data ;
		head = head.next ;
		size-- ;
		return obj ;
	}
	
	//查找指定元素,找到了返回节点Node,找不到返回null
	public Node find(Object obj){
		Node current = head ;
		int tempSize = size ;
		while(tempSize > 0){
			if(obj.equals(current.data)){
				return current ;
			}else{
				current = current.next;
			}
			tempSize--;
		}
		return null;
	}
	
	//删除指定元素,删除成功返回true
	public boolean delete(Object value){
		if(size ==0){
			return false;
		}
		Node current = head ;  //当前节点
		Node previous = head ; //之前的节点
		while(current.data != value){
			if(current.next == null){
				return false ;
			}else{
				previous = current ;
				current = current.next ;
			}
		}
		//如果删除的节点是头节点
		if(current ==head){
			head = current.next;
			size--;
		}else{
			previous.next =current.next ;
			size--;
		}
		return true ;
	}
	
	//判断链表是否为空
	public boolean isEmpty(){
		return size==0 ;
	}
	
	//显示节点信息
	public void display(){
		if(size>0){
			Node node = head ;
			int tempSize = size ;
			if(tempSize==1){//当前链表只有一个节点
				System.out.println("["+node.data+"]");
				return ;
			}
			while(tempSize>0){
				if(node.equals(head)){
					System.out.print("["+node.data+"->");
				}else if(node.next ==null){
					System.out.print(node.data+"]");
				}else{
					System.out.print(node.data+"->");
				}
				node = node.next ;
				tempSize-- ;
			}
			System.out.println();
		}else{
			System.out.println("[]");
		}
	}
	//测试
	public static void main(String args[]){
		SingleLinkedList singleList = new SingleLinkedList();
		singleList.addHead("A") ;
		singleList.addHead("B") ;
		singleList.addHead("C") ;
		//打印当前链表
		singleList.display();
		
		//删除B
		singleList.delete("B") ;
		
		singleList.display();
		
		System.out.println(singleList.find("A"));
	}
}

更详细链表讲解及转自https://www.cnblogs.com/ysocean/p/7928988.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值