单向链表特点实现

//结点

public class LinkNode {
	
	public int value;//值
	public String key;//关键字
	public LinkNode nextNode = null;
	
	public LinkNode(int value,String key){
		this.value = value;
		this.key = key;
	}
	
	public LinkNode(){
		
	}
	
	public void println(){
		System.out.println("结点值:" + value+"   结点key "+key);
	}

}

//链表类

public class LinkList {
	
	LinkNode headNode = new LinkNode();//头结点,只保存列表中第一个结点的引用
	LinkNode tempLinkNode;//临时保存当前的操作的结点
	
	//判断是否是空链表,如果头结点没有next地址为空,则是空链表
	public boolean isEmpty(){
		return headNode.nextNode == null?true:false;
	}
	
	//添加结点(返回添加的结点)
	public LinkNode AddNode(int value){
		LinkNode linkNode = null;
		if (isEmpty()) {
			linkNode = new LinkNode(value, value+"");
			headNode.nextNode = linkNode;
			tempLinkNode = linkNode;
		}else{
			linkNode = new LinkNode(value, value+"");
			tempLinkNode.nextNode = linkNode;
			tempLinkNode = linkNode;
		}
		return tempLinkNode;
	}
	
	public LinkNode removeFirst(){
		
		LinkNode tempLinkNode = headNode.nextNode; 
		headNode.nextNode = tempLinkNode.nextNode;
		
		return null;
		
	}
	
	//查找指定结点
	public LinkNode findLinkNode(String findkey){
		
		LinkNode  currentNode = headNode.nextNode;
		
		while(true){
			if (currentNode == null) {
				System.out.println("没有找到该结点");
				return null;
			}
			
			if (currentNode.key.equals(findkey)) {
				System.out.println("找到的结点:值为"+currentNode.value + " key为"+currentNode.key);
				return currentNode;
			}
			currentNode = currentNode.nextNode;
		}
		
	}
	
	//删除结点     
	public LinkNode delNode(String delkey){
		
		LinkNode privousNode = null;//当前结点的上一个结点
		LinkNode currentNode = null;//当前结点
		
		LinkNode firstNode = headNode.nextNode;//第一个结点
		
		currentNode = firstNode;
		privousNode = firstNode;
		
		while (currentNode != null && !currentNode.key.equals(delkey)) {
		    
		    privousNode = currentNode;
		    currentNode = currentNode.nextNode;
		}
		
		if (currentNode == null) {
	    	System.out.println("没有找到想要删除的数据");
			return null;
		}
		
        privousNode.nextNode = currentNode.nextNode;//如果删除的是最后一个currentNode.nextNode == null;  
        
		return currentNode;
	}
	
	//列表末尾插入
	public LinkNode addLastNode(int value){
		 
		LinkNode linkNode = new LinkNode(value, value+"");
		linkNode.nextNode = null;
		tempLinkNode.nextNode = linkNode;
		tempLinkNode = linkNode;
		return linkNode;
	}
	
	public LinkNode addPositionNode(int position){
		
		return null;
	}
	
	//遍历结点
	public void lookLinkList(){
		
		LinkNode currentLinkNode = headNode.nextNode;
		while (currentLinkNode != null) {
			System.out.println("结点值:  "+ currentLinkNode.value);
			currentLinkNode = currentLinkNode.nextNode;
		}
		
	}
	
}
//展示

public static void main(String[] args) {
		
		LinkList linkList = new LinkList();
		for (int i = 0; i < 10; i++) {
			linkList.AddNode(i);
		}
		
		//遍历,必须是从头部一直向下遍历 
		linkList.lookLinkList();
		
		//查找指定元素
		System.out.println("\n查找指定元素");
		linkList.findLinkNode("3");
		
		//删除指定元素再遍历
		System.out.println("\n遍历元素");
		linkList.delNode("9");
		linkList.lookLinkList();
		
		//删除第一个元素
		System.out.println("\n删除列表第一个元素");
		linkList.removeFirst();
		linkList.lookLinkList();
		
		//末尾添加元素
		System.out.println("\n末尾添加一个元素");
		for (int i = 10; i < 15; i++) {
			linkList.addLastNode(i);
		}
		linkList.lookLinkList();
	}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值