zoukankan      html  css  js  c++  java
  • Java数据结构——有序链表

    //=================================================
    // File Name       :	SortedList_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    //类名:SortedList
    //属性:
    //方法:
    class SortedList{
    	private Link_long first;
    	
    	public SortedList(){			//构造函数
    		first = null;
    	}
    	
    	public void insert(long key){
    		Link_long newLink = new Link_long(key);
    		Link_long previous = null;		//上一次插入的值
    		Link_long current = first;			//每插入一次,current就重新赋为表头的值
    		while(current != null && key > current.dData){	//没进入这里,pre就是null,也就只进入下面if的上一层
    			previous = current;
    			current = current.next;			//current的位置往后移动
    		}
    		if(previous == null){				//最开始的情况,给first赋值为newLink,即key
    			first = newLink;
    		}else{											//
    			previous.next = newLink;
    		}
    		newLink.next = current;
    	}
    	
    	public long remove(){
    		Link_long temp = first;
    		first = first.next;
    		return temp.dData;
    	}
    	
    	public void displayList(){
    		System.out.println("List (first-->last)");
    		Link_long current = first;
    		while(current != null){
    			current.displayLink();
    			current = current.next;
    		}
    	}
    	
    }
    
    
    //主类
    //Function        : 	SortedList_demo
    public class SortedList_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		SortedList theSortedList = new SortedList();
    		theSortedList.insert(10);
    		theSortedList.insert(20);
    		theSortedList.insert(30);
    		theSortedList.displayList();
    		theSortedList.remove();
    		theSortedList.remove();
    		theSortedList.displayList();
    	}
    
    }
    

     

  • 相关阅读:
    Java Map遍历方式的选择
    UriMatcher类的addURI()方法
    Java IO流分析整理[转]
    java基础一些注意细节
    java中static变量和方存在内存什么区域
    详细解析Java中抽象类和接口的区别
    mybatis一些记录
    Go语言简介(上)— 语法
    JavaScript相关-深入面向对象
    33个组件5
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5339156.html
Copyright © 2011-2022 走看看