美文网首页
手写单链表

手写单链表

作者: _一叶孤帆 | 来源:发表于2021-06-28 15:36 被阅读0次
    public class LinkedList<T> {
    
        // 节点信息
        class Node {
            T data;
            Node next;
    
            public Node (T data, Node node){
                this.data = data;
                this.next = node;
            }
        }
        
        Node list; //单链表头部
        int size; //链表有多少个节点
    
        public LinkedList() {
            size = 0;
        }
    
        // 添加节点
        // 添加头结点
        public void put(T data){
            Node head = list;
            Node curNode = new Node(data,list);
            list = curNode;
            size++;
        }
    
    
        public void put(int index, T data) {
            checkPositionIndex(index);
            Node cur = list;
            Node head = list;
            for (int i = 0; i < index; i++) {
                head = cur;
                cur = cur.next;
            }
            Node = Node(data,cur);
            head.next = node;
            size++;
        }
    
        // 删除节点
        // 删除头部节点
        public T remove(){
            if(!list) return null;
            Node head = list;
            list = list.next;
            head.next = null; // 促进 GC
            size--;
            return head.data;
        }
    
        public T remove(int index) {
            checkPositionIndex(index);
            Node cur = list;
            Node head = list;
    
            for (int i = 0; i < index; i++) {
                head = cur;
                cur = cur.next;
            }
    
            head.next = cur.next;
            cur.next = null;
            size--;
            return cur.data;
        }
    
        public T removeLast() {
            
            if(list != null) {
                Node node = list;
                Node cur = list;
    
                while(cur.next != null) {
                    node = cur;
                    cur = cur.next;
                }
    
                node.next = null;
                size --;
                return cur.data;
            }
            return null;
        }
        // 修改节点
    
        public void set(int index, T newData) {
            checkPositionIndex(index);
            Node head = list;
            for (int i = 0; i < index; i++) {
                head = head.next;
            }
            head.data = newData;
        }
    
        // 查询节点
        // 头结点
        public T get(){
            
            if(list != null) {
                return list.data;
            }
            return null;
        }
    
        public T get(int index) {
            checkPositionIndex(index);
            Node head = list;
            for (int i = 0; i < index; i++) {
                head = head.next;
            }
            return head.data;
        }
    
        private void checkPositionIndex(int index) {
            if(!(index >= 0 && index <= size)) {
                // 抛出错误
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:手写单链表

          本文链接:https://www.haomeiwen.com/subject/tsiqultx.html