JAVA学习记录(十一)—— 无头双向非循环链表

博客介绍了双向链表与单向链表的区别。单向链表结点包含当前节点值和指向下个结点的地址,双向链表结点除这两项外,还包含指向上一个结点的地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

双向链表与单向链表的区别是,单向链表的一个结点包含(当前节点的值val、指向下个结点的地址next);双向链表的一个结点包含(当前节点的值val、指向下个节点的地址Next、指向上一个结点的地址prev)

class ListNode(){
    // 也可以使用public进行修饰
    // 但是双链表使用private,为了熟悉一下使用get和set操作
    private int val;
    private ListNode next;
    private ListNode prev;
    // 生成构造方法
    public List(int val){this.val = val;}
    // 创建节点中三个部分各自的get和set函数
    public void setVal(int val){
        this.val = val;
    }
    public void getVal(){return next;}
    
    public ListNode setNext(ListNode next){
        this.next = next;
    }
    public void getNext(){return prev;}
    
    public ListNode setPrev(NodeList prev){
        this.prev = prev;
    }
    public void getPrev(){return prev;}
}
public class twoWayLinkList{
    // 与单向链表不同的是,单向链表只有一个指针head
    //                   双向链表有两个指针,一个在表头head,和表尾last;
    private ListNode head;
    private ListNode last;
    
    // 打印链表
    public void display(){
        ListNode cur = this.head;
        while(cur != null){
        // 使用了private修饰后,想要访问该结点的val不能之间“点”出来,需要使用get方法
            System.out.println(cur.getVal() + " ");
            cur = cur.getNext();
        }
        System.out.println();
    }
    // 头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            this.last = node;
        }else{
            node.setNext(this.head);
            node.setPrev(node);
            this.head = node;
        }
    }
    // 尾插法
    public void addLast(int data){
        ListNode noda = new ListNode(data);
        if(this.head == null){
            this.head == node;
            this.last = node;
        }else{
            this.last.setNext(node);
            node.setPrev(this.last);
            this.last = node;
        }
    }
    // 求链表长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            cur = cur.getNext();
            count++;
        }
        return count;
    }
    // 任意位置插入,第一个数据节点为0下标
    public void addIndex(int index, int data){
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size){
            addList(data);
            return;
        }
        ListNode cur = this.head;
        while(cur != 0){
            cur = cur.getNex();
            index--;
        }
        ListNode node = new ListNode(data);
        node.setNext(cur);
        node.setPrev(cur.getPrev());
        node.getPrev().setNote(node);
        cur.setPrev(node);
    }
    // 查找关键字key是否在链表中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.getVal == key){
                return true;
            }
            cur = cur.gerNext();
        }
        return false;
    }
    // 找到第一次出现关键字key的结点
    public ListNode findKeyNode(int key){
        ListNode cur = this.head;
        while(while != null){
             if(cur.getVal == key){
                  return cur;
             }
             cur = getNext();
        }
        return null;
    }
    // 删除第一次出现关键字key的节点
    public void remove(int key){
        ListNode cur = this.findKeyNode(key);
        if(cur == null){
            return;
        }
        if(cur == this.head){
            this.head = this.head.getNext();
            this.head.setPrev(null);
        }
        if(cur == this.last){
            cur.setPrev().setNext(null);
            this.last = this.last.getPrev();
            return;
        }
        cur.getPrev().setNext(cur.getNext());
        cur.getNext().setPrev(cur.getPrev());
    }
    // 删除所有值为key的结点
    public void removeAllKey(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.getVal() == key){
                if(cur == this.head){
                    this.head = this.head.getNext();
                    this.head.setPrev(null);
                }else{
                    cur.getPrev().setNext(cur.getNext());
                    if(cur.getNext() != null){//是不是尾结点
                        cur.getNext().setPrev(cur.getPrev());
                    }else{
                        this.last = this.last.getPrev();
                    }
                }
            }
            cur = cur.getNext();
        }
    }
    // 清空链表
    public void clear(){
        this.head = null;
        this.last = null;
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值