• Java数据结构——链表-单链表


    <1>链表

    <2>引用和基本类型

    <3>单链表

    //=================================================
    // File Name       :	LinkList_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    //类名:Link
    //属性:
    //方法:
    class Link{			//链节点类
    	public int iData;
    	public double dData;
    	public Link next;				//链表中下一个节点的引用
    	
    	public Link(int iData, double dData) {
    		super();
    		this.iData = iData;
    		this.dData = dData;
    	}
    	
    	public void displayLink(){			//显示当前节点的值
    		System.out.println("iData="+iData+","+"dData"+dData);
    	}
    	
    }
    
    //类名:LinkList
    //属性:
    //方法:
    class LinkList{
    	private Link first;			//只需要第一个节点,从第一个节点出发即可定位所有节点
    
    	public LinkList() {			//构造函数
    		this.first = null;
    	}
    	
    	public boolean isEmpty(){
    		return (first == null);
    	}
    	
    	public void insertFirst(int id,double dd){		//插入元素是从链表的头开始插入
    		Link newLink = new Link(id,dd);
    		newLink.next = first;
    		first = newLink;
    	}
    	
    	public Link deleteFirst(){
    		Link temp = first;		//暂存first
    		first = first.next;			//把next设为first
    		return temp;				//返回原来的first
    	}
    	
    	public void displayList(){
    		System.out.println("List(first-->last):");
    		Link current = first;			//用于不断改变位置实现遍历
    		while(current != null){
    			current.displayLink();
    			current = current.next;
    		}
    	}
    }
    
    //主类
    //Function        : 	LinkList_demo
    public class LinkList_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		LinkList theList = new LinkList();
    		theList.insertFirst(11, 11.1);
    		theList.insertFirst(22, 22.2);
    		theList.insertFirst(33, 33.3);
    		theList.insertFirst(44, 44.4);
    		theList.insertFirst(55, 55.5);
    		theList.displayList();
    		while(!theList.isEmpty()){
    			Link aLink = theList.deleteFirst();
    			System.out.print("delete:");
    			aLink.displayLink();
    		}
    		theList.displayList();
    	}
    
    }
    
    //=================================================
    // File Name       :	LinkList_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    //类名:Link
    //属性:
    //方法:
    class Link{			//链节点类
    	public int iData;
    	public double dData;
    	public Link next;				//链表中下一个节点的引用
    	
    	public Link(int iData, double dData) {
    		super();
    		this.iData = iData;
    		this.dData = dData;
    	}
    	
    	public void displayLink(){			//显示当前节点的值
    		System.out.println("iData="+iData+","+"dData"+dData);
    	}
    	
    }
    
    //类名:LinkList
    //属性:
    //方法:
    class LinkList{
    	private Link first;			//只需要第一个节点,从第一个节点出发即可定位所有节点
    
    	public LinkList() {			//构造函数
    		this.first = null;
    	}
    	
    	public boolean isEmpty(){
    		return (first == null);
    	}
    	
    	public void insertFirst(int id,double dd){		//插入元素是从链表的头开始插入
    		Link newLink = new Link(id,dd);
    		newLink.next = first;
    		first = newLink;
    	}
    	
    	public Link deleteFirst(){
    		Link temp = first;		//暂存first
    		first = first.next;			//把next设为first
    		return temp;				//返回原来的first
    	}
    	
    	public void displayList(){
    		System.out.println("List(first-->last):");
    		Link current = first;			//用于不断改变位置实现遍历
    		while(current != null){
    			current.displayLink();
    			current = current.next;
    		}
    	}
    	
    	public Link find(int key){					//查找指定的关键字
    		Link current = first;
    		while(current.iData != key){
    			if(current.next == null)
    				return null;
    			else
    				current = current.next;
    		}
    		return current;
    	}
    	
    	public Link delete(int key){			//如果current的值匹配,则删除
    		Link current = first;				
    		Link previous = first;
    		//没有匹配到值
    		while(current.iData != key){
    			if(current.next == null)
    				return null;
    			else{							//pre和cur向后移动
    				previous = current;
    				current = current.next;
    			}
    		}
    		//匹配到值
    		if(current == first)		//只有一个first,并匹配,则把first设成first.next
    			first = first.next;
    		else								//current的值匹配,则删除,并把cur的next赋给pre的next
    			previous.next = current.next;
    		return current;
    	}
    }
    
    //主类
    //Function        : 	LinkList_demo
    public class LinkList_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		LinkList theList = new LinkList();
    		theList.insertFirst(11, 11.1);
    		theList.insertFirst(22, 22.2);
    		theList.insertFirst(33, 33.3);
    		theList.insertFirst(44, 44.4);
    		theList.insertFirst(55, 55.5);
    		theList.displayList();
    		
    		Link f = theList.find(22);
    		if(f != null){
    			System.out.print("找到:");
    			f.displayLink();
    		}
    		else
    			System.out.print("没有找到");
    		
    		Link d = theList.delete(32);
    		if(d != null){
    			System.out.print("删除:");
    			d.displayLink();
    		}
    		else
    			System.out.print("没有找到匹配的删除");
    		
    	}
    
    }
    
  • 相关阅读:
    Python基础----内置函数补充、匿名函数、递归函数
    为arm-linux开发板挂载基于nfs的根文件系统
    vsftp上传553 Could not create file错误解决
    在itop4412移植linux4.14和设备树遇到的问题及解决
    itop4412uboot中支持usbhub
    上下界网络流
    lca(最近公共祖先(在线)) 倍增法详解
    lca(最近公共祖先(离线))
    最小费用最大流
    spfa模板+讲解
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5337004.html
Copyright © 2020-2023  润新知