链表 带/不带头节点实现 java

带头节点

/**
 * 带头节点的单链表的实现
 * @param <T>
 */
class Link<T extends Comparable<T>>{

    /**
     * 指向单链表的头节点,其地址域中记录了链表第一个节点的地址
     */
    HeadEntry<T> head;

    /**
     * 初始化链表,生成头节点被head指向
     */
    public Link(){
        this.head = new HeadEntry<>(0, null, null);
    }

    /**
     * 单链表的头插法
     * @param val
     */
    public void insertHead(T val){
        Entry<T> node = new Entry<>(val, this.head.next);
        this.head.next = node;
        this.head.cnt += 1; // 更新头节点中链表节点的个数
    }

    /**
     * 单链表的尾插法
     * @param val
     */
    public void insertTail(T val){
        Entry<T> node = head;
        while(node.next != null){
            node = node.next;
        }
        node.next = new Entry<>(val, null);
        this.head.cnt += 1; // 更新头节点中链表节点的个数
    }

    /**
     * 单链表中删除所有值是val的节点
     * @param val
     */
    public void remove(T val){
        Entry<T> pre = head;
        Entry<T> cur = head.next;

        while(cur != null){
            if(cur.data == val){
                // val节点的删除
                pre.next = cur.next;
                cur = pre.next; // 重置cur,继续向后删除链表中所有值为val的节点
                this.head.cnt -= 1; // 更新头节点中链表节点的个数
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
    }

    /**
     * 打印单链表的所有节点元素的值
     */
    public void show(){
        Entry<T> cur = this.head.next;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 获取链表节点的个数
     * @return
     */
    public int size() {
        return this.head.cnt;
    }

    /**
     * 单链表的节点类型
     * @param <T>
     */
    static class Entry<T>{
        T data; // 链表节点的数据域
        Entry<T> next; // 下一个节点的地址

        public Entry(T data, Entry<T> next) {
            this.data = data;
            this.next = next;
        }
    }

    /**
     * 头节点的类型,添加了int cnt成员变量,记录链表中节点的个数
     * @param <T>
     */
    static class HeadEntry<T> extends Entry<T>{
        int cnt; // 用来记录节点的个数

        public HeadEntry(int cnt, T data, Entry<T> next) {
            super(data, next);
            this.cnt = cnt;
        }
    }
}

不带头节点

int length=-1;//链表长度
    Node first;//“首”节点

    //节点类型
    class Node{
        int val;
        Node next;
        public Node(){
            //这个是用来申请头节点时候用的
        }
        public Node(int val){
            this.val=val;
        }
    }


    /** Initialize your data structure here. */
    public MyLinkedList() {
      //  first=new Node();
    }

    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
        index--;
        if(index>length){
            return -1;
        }
        Node newnode=first;
        for(int cur=-1;cur<=length;cur++){
            if(cur==index){
                return newnode.val;
            }
            newnode=newnode.next;
        }
        return -1;
    }

    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void addAtHead(int val) {
        //这个为不带头节点的单链表所以头插要将“首”节点更新
        Node newnode=new Node(val);
        if(length<0){
            first=newnode;
        }else{
            newnode.next=first;
            first=newnode;
        }
        length++;
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {//尾插法
        length++;
        Node newnode=first;
        if(newnode==null){
            newnode=new Node(val);
            first=newnode;
            return;
        }
        while(newnode.next !=null){
            newnode=newnode.next;
        }
        newnode.next=new Node(val);

    }

    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
        index--;
        if(index==length){
            addAtTail(val);
            return;
        }else if(index>length){
            return;
        }else if(index<0){
            addAtHead(val);
            return;
        }
        Node pre=first;
        Node second=first.next;
        Node newnode=new Node(val);
        for(int cur=-1;cur<=length;cur++){
            if(cur==index){
                pre.next=newnode;
                newnode.next=second;
                length++;
            }
            pre=pre.next;
            newnode=newnode.next;
        }
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if(index>length){
            return;
        }else if(first.next==null){//如果只有一个首节点
            first=null;
            length--;
        }
        Node pre=first;
        Node second=first.next;
        for(int cur=0;cur<=length;cur++){
            if(cur==index){
                pre.next=second.next;
                length--;
            }
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值