• java学习之—链表(1)


    /**
     * 单链表操作
     * Create by Administrator
     * 2018/6/14 0014
     * 下午 2:05
     **/
    public class Link {
    
        public int iData;
        public double dData;
        public Link next;
    
        public Link(int id, double dd) {
            this.iData = id;
            this.dData = dd;
        }
    
        public void displayLink() {
            System.out.print("{" + iData + " , " + dData + "} ");
        }
    }
    
    class LinkList {
    
        private Link first;
    
        public LinkList() {
            this.first = null;
        }
    
        public boolean isEmpty(){           //判断链表是否为空
            return (first == null);
        }
    
        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){        //删除指定对象
            Link current = first;
            Link previous = first;
            while (current.iData != key){
                if(current.next == null){
                    return null;
                }else {
                    previous = current;
                    current = current.next;
                }
            }
            if(current == first){
                first = first.next;
            }else {
                previous.next = current.next;
            }
            return current;
        }
    
        public void insertFirst(int id,double dd){
            Link link = new Link(id, dd);
            link.next = first;          //储存对象的引用
            first = link;               //把当前对象赋值给first
        }
    
        public Link deleteFrst() {      //删除首链表,正好和添加相反
            if(!isEmpty()){
                Link temp = first;
                first = first.next;
                return temp;
            }
            return null;
        }
    
        public void displayList(){
            System.out.print("List (first-->last): ");
            Link current = first;
            while(current != null){
                current.displayLink();
                current = current.next;
            }
            System.out.println("");
        }
    
        public static void main(String[] args) {
            LinkList theList = new LinkList();
    
            theList.insertFirst(22,2.99);
            theList.insertFirst(44,4.99);
            theList.insertFirst(66,6.99);
            theList.insertFirst(88,8.99);
    
            theList.displayList();
    
    //        while (!theList.isEmpty()){
    //            Link aLink = theList.deleteFrst();
    //            System.out.print("Deleted");
    //            aLink.displayLink();
    //            System.out.println("");
    //        }
    
            Link f = theList.find(44);
            if(f != null){
                System.out.print("find Link: ");
                f.displayLink();
                System.out.println("");
                Link result = theList.delete(f.iData);
                if(result != null){
                    System.out.print("delete success ");
                    result.displayLink();
                }
            }
            System.out.println("");
            theList.displayList();
        }
    }
    

      

  • 相关阅读:
    高级I/O之存储映射I/O
    高级I/O之readn和writen函数
    高级I/O之readv和writev函数
    高级I/O之异步I/O
    高级I/O之I/O多路转接——pool、select
    高级I/O之STREAMS
    高级I/O之记录锁
    高级I/O之非阻塞I/O
    用于守护进程的出错处理函数
    守护进程之客户进程-服务器进程模型
  • 原文地址:https://www.cnblogs.com/chancy/p/9183288.html
Copyright © 2020-2023  润新知