zoukankan      html  css  js  c++  java
  • 线性表java实现之单链表存储源码

    class LinkList<T>{
        private class Node{
            private T data;
            private Node next;
            public Node(T data,Node next){
                this.data = data;
                this.next = next;
            }
        }
        private int length=0; //链表长度
        private Node head;
        private Node tail;
        public LinkList(){
            head=null;
            tail=null;
        }
        
        public LinkList(T data){
            head = new Node(data, null);
            head = tail;
            length++;
        }
        
        public int getLength(){
            return length;
        }
        
        //插入
        /**
         * @param data
         * @param location 从1开始
         */
        public void insert(T data,int location){
            if(location<0 || location >length)
                throw new IndexOutOfBoundsException("越界");
                        
            //如果本来为空表
            if(head == null){
                add(data);
            }else{
                //非空,但插在第一个位置
                if(location == 1){
                    head = new Node(data,head);
                }else{
                    int flag = 1;
                    Node temp = head;
                    //得到所要插入位置的节点前一个节点
                    while(flag < location-1){
                        temp = temp.next;
                        flag++;
                    }
                    temp.next = new Node(data, temp.next);
                
                }
            }
            length++;
        }
        
        public void add(T data){
            if(head == null){
                head = new Node(data, null);
                tail = head;
                
            }else{
                tail.next = new Node(data,null);
                tail = tail.next;
            }
            length++;
        }
        
        //删除
        public void delete(int location){
            if(location>length || location<0){
                throw new IndexOutOfBoundsException("越界");
            }
            int index=1;
            Node prev = head;
            //得到前一节点
            while(index<location-1){
                prev = prev.next;
                index++;
            }
            Node temp = prev.next;
            prev.next = prev.next.next;
            temp.next = null;//将删除的节点一下链接置空
            length--;
        }

        //查找
        public T get(int index){
            T data=null;
            if(index<=0 || index>length)
                throw new IndexOutOfBoundsException("越界");
            int flag = 1;
            Node temp = head;
            while(flag<index){
                temp = temp.next;
                flag++;
            }
            data = temp.data;
            return data;
        }
        public boolean isEmpty(){
            if(length == 0){
                return true;
            }else{
                return false;
            }
        }
        @Override
        public String toString() {
            if(length == 0){
                return "[]";
            }else{
                StringBuffer sb = new StringBuffer("[");
                
                Node temp = head;
                while(temp!=null){
                    sb.append(temp.data.toString()+",");
                    temp = temp.next;
                }
                return sb.delete(sb.length()-1, sb.length()).toString()+"]";
            }
        }
        
        
    }

  • 相关阅读:
    Checking Types Against the Real World in TypeScript
    nexus pip proxy config
    go.rice 强大灵活的golang 静态资源嵌入包
    几个golang 静态资源嵌入包
    rpm 子包创建学习
    Rpm Creating Subpackages
    ava 类似jest snapshot 功能试用
    ava js 测试框架基本试用
    The Architectural Principles Behind Vrbo’s GraphQL Implementation
    graphql-compose graphql schema 生成工具集
  • 原文地址:https://www.cnblogs.com/mapleyuan/p/3002741.html
Copyright © 2011-2022 走看看