算法通关村第一关——链表青铜挑战笔记

文章详细介绍了如何使用Java构建单向链表,包括获取链表长度、在链表首部、中间和尾部插入节点以及删除节点的方法。同时,文章还讨论了双向链表的构造,插入和删除元素的实现,强调了双向链表可以双向遍历但操作更为复杂的特点。

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

1.理解java是如何构造出链表的?

 结点数据结构与方法如下:

public class Node {
    public int val;
    public Node next;
    Node(int x){
        val = x;
        next = null;
    }

    public static int getListLength(Node head){
        int length = 0;
        Node node = head;
        while (node!=null){
            length++;
            node=node.next;
        }
        return length;
    }

        public static Node insertNode(Node head,Node nodeInsert,int position){
        if (head==null){
            return nodeInsert;
        }
        int size = getListLength(head);
        if (position>size+1||position<1){
            System.out.println("位置参数越界");
            return head;
        }
        if (position==1){
            nodeInsert.next = head;
            head = nodeInsert;
            return head;
        }
        Node pNode = head;
        int count = 1;
        while(count < position-1){
            pNode = pNode.next;
            count++;
        }
        nodeInsert.next=pNode.next;
        pNode.next=nodeInsert;

        return head;
    }

    public static Node deleteNode(Node head,int position){
        if (head == null){
            return null;
        }
        int size = getListLength(head);
        if (position > size||position<1){
            System.out.println("位置参数错误");
            return head;
        }
        if (position==1){
            return head.next;
        }else {
            Node preNode = head;
            int count = 1;
            while (count<position-1){
                preNode = preNode.next;
                count++;
            }
            Node curNode = preNode.next;
            preNode.next = curNode.next;
        }
        return head;
    }


2.链表增加元素,首部、中间和尾部分别会有什么问题,该如何处理?


        链表进行插入元素时通过Node head、node nodeInsert、int position分别得到链表头(整个链表)、要插入的结点、要插入的位置。
        若插入位置错误,则返回链表;若插入位置为1(即表头),需将插入结点的next置为链表头,将链表头置为插入结点,返回链表;若插入位置在1之后,需先得到插入位置的前一个结点pNode,pNode的next置为插入结点,插入结点的next置为pNode的next,返回链表。

3.链表删除元素,首部、中间和尾部分别会有什么问题,该如何处理?


        删除链表需要通过Node head、int position获得链表头(整个链表)、删除位置。
若删除位置错误,则返回链表;若删除位置为1(表头),则直接返回head的next;若删除位置在1之后,需获取删除位置前一个结点pNode,pNode的next置为pNode的next的next,返回链表。


4.双向链表是如何构造的,如何实现元素的插入和删除?
 

结点数据结构如下:

public class DoubleNode {
    public int data;
    public DoubleNode next;
    public DoubleNode prev;
    public DoubleNode(int data){
        this.data = data;
    }

    public void displayNode(){
        System.out.println("{" + data + "} ");
    }
}


链表结构与方式如下:



//链表结构与方向如下:

import org.junit.Test;

public class DoubleLinkList {
    private DoubleNode first;
    private DoubleNode last;
    public DoubleLinkList(){
        first = null;
        last = first;
    }

    public void displayForward(){
        System.out.println("List(first-->last):");
        DoubleNode current = first;
        while (current!=null){
            current.displayNode();
            current = current.next;
        }
        System.out.println();
    }

    public void displayBackward(){
        System.out.println("List(last-->first):");
        DoubleNode current = last;
        while (current!=null){
            current.displayNode();
            current = current.prev;
        }
        System.out.println();
    }

    public void insertFirst(int data){
        DoubleNode newDoubleNode = new DoubleNode(data);
        if (isEmpty()){
            last = newDoubleNode;
        }else {
            first.prev = newDoubleNode;
        }
        newDoubleNode.next = first;
        first = newDoubleNode;
    }

    public void insertLast(int data){
        DoubleNode newDoubleNode = new DoubleNode(data);
        if (isEmpty()){
            first = newDoubleNode;
        }else {
            newDoubleNode.prev = last;
            last.next = newDoubleNode;
        }
        last = newDoubleNode;
    }
    //从某个结点后插入
    public void insertAfter(int key, int data){
        DoubleNode newDaoubleNode = new DoubleNode(data);
        DoubleNode current = first;
        while ((current!=null)&&(current.data!=key)){
            current = current.next;
        }
        if (current ==null){
            if (isEmpty()){//链表为空
                first = newDaoubleNode;
                last = newDaoubleNode;
            }else {//找不到特定值则插入末尾
                last.next = newDaoubleNode;
                newDaoubleNode.prev = last;
                last = newDaoubleNode;
            }
        }else {
            if (current == last){//key值与最后结点的data相等,last将为newDaoubleNode
                newDaoubleNode.next = null;
                last = newDaoubleNode;
            }else {//在两结点中间插入
                newDaoubleNode.next = current.next;
                current.next.prev = newDaoubleNode;
            }
            current.next = newDaoubleNode;
            newDaoubleNode.prev =current;
        }
    }

    public DoubleNode deleteFirst(){
        DoubleNode temp = first;
        if (first.next == null){
            last = null;
        }else {
            first.next.prev = null;
        }
        first = first.next;
        return temp;
    }

    public DoubleNode deleteLast(){
        DoubleNode temp = last;
        if (first.next ==null){
            first = null;
        }else {
            last.prev.next = null;
        }
        last = last.prev;
        return temp;
    }

    public DoubleNode deleteKey(int key){
        DoubleNode current = first;
        while (current != null&&current.data!=key){
            current = current.next;
        }
        if (current==null){
            return null;
        }else {
            if (current == first){
                first = current.next;
                current.next.prev = null;
            }else if(current==last){
                last = current.prev;
                current.prev.next = null;
            }else {
                current.prev.next = current.next;
                current.next.prev = current.prev;
            }
        }
        return current;
    }

    public boolean isEmpty(){
        return (first==null);
    }
    @Test
    public void test(){
        DoubleLinkList theList = new DoubleLinkList();
        theList.insertFirst(20);
        theList.insertFirst(40);
        theList.insertFirst(60);
        theList.insertLast(10);
        theList.insertLast(30);
        theList.insertLast(50);

        theList.displayForward();
        theList.displayBackward();

        theList.deleteFirst();
        theList.deleteLast();
        theList.deleteKey(10);

        theList.displayForward();

        theList.insertAfter(20, 70);
        theList.insertAfter(30, 80);

        theList.displayForward();
        theList.displayBackward();
    }



        双向链表结点较于链表结点多出来指向前一个结点的DoubleNode prev,可进行双向的查询但增删复杂;双向链表拥有头结点first和尾结点last。
增加结点:
        若增加在双向链表头,且链表为空,则first与last均为插入结点;链表不为空,则first的prev置为插入结点,插入结点的next置为first,再令first置为插入结点。
        若增加在表尾,且链表为空,则first与last均为插入结点;链表不为空,则last的next置为插入结点,插入结点的prev置为last,再令last置为插入结点。
        在特定结点后插入结点(通过data的值(key)找到特定结点),在链表中进行遍历,寻找data等于key的结点DoubleNode current。若current为null,且链表为空,则first=last=插入结点;链表不为空,则说明未找到特定结点,插入结点到链表尾。若current不为null说明找到特定结点,且current等于last,插入结点到链表尾;current不等于last,则current的next的prev为插入结点,插入结点的next为current的next,插入结点的prev为current,current的next为插入结点。
删除结点:
        若删除双向链表头,且链表为空,则first与last均为null;链表不为空,则first的next置为null,first置为first的next。
        若删除表尾,且链表为空,则first与last均为null;链表不为空,则last的prev的next置为null,last置为last的prev。
        删除特定结点(通过data的值(key)找到特定结点),在链表中进行遍历,寻找data等于key的结点DoubleNode current。若current为nul,则说明未找到特定结点,返回null。若current不为null说明找到特定结点,且current等于first,删除表头;current等于last,删除表尾;此外,则current的next的prev为current的prev,current的prev的next为current的next,返回current。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值