题解 | #【模板】链表#

【模板】链表

https://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f

import java.util.Scanner;

/**
 * @BelongsProject: homework
 * @Author: Ling xi_Li
 * @CreateTime: 2023-10-25 12-40
 * @Description: TODO
 */

class Node {
    private int data;
    private Node next;

    public Node() {
        this.data = 0;
        this.next = null;
    }

    public Node(int data) {
        this.data = data;
        this.next = null;
    }

    public Node getNext() {
        return next;
    }

    public int getData() {
        return data;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

class Link {

    //TODO head是带空节点的头指针
    private Node head;

    public Link() {
        head = null;
    }

    public Link(Node head) {
        this.head = head;
    }

    public boolean isEmptyLink() {
        return head.getNext() == null;
    }

    public void append(Node node) {
        if (isEmptyLink()) {
            this.head.setNext(node);
        } else {
            Node temp = this.head.getNext();
            while (temp.getNext() != null) {
                temp = temp.getNext();
            }
            temp.setNext(node);
        }
    }

    public void add(Node node) {
        if (isEmptyLink()) {
            this.head.setNext(node);
        } else {
            node.setNext(this.head.getNext());
            this.head.setNext(node);
        }
    }

    public void insert(int x, int y) {
        Node node = new Node(y);

        int flag = 0;
        Node temp = this.head;
        while (temp.getNext() != null) {
            if (temp.getNext().getData() == x) {
                node.setNext(temp.getNext());
                temp.setNext(node);
                flag = 1;
                break;
            }
            temp = temp.getNext();
        }
        if (flag == 0) {
            temp.setNext(node);
        }
    }

    public void remove(int num) {

        if (isEmptyLink()) return;

        Node temp = this.head;

        Node current = temp.getNext();

        while (current != null) {
            if (current.getData() == num) {
                temp.setNext(current.getNext());
                current.setNext(null);
                break;
            }
            temp = current;
            current = current.getNext();
        }
    }

    public void show() {

        if (isEmptyLink()) {
            System.out.println("NULL");
            return;
        }
        Node temp = this.head.getNext();
        while (temp != null) {
            System.out.print(temp.getData() + " ");
            temp = temp.getNext();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别

        Link list = new Link(new Node());
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            String op = in.next();
            if (op.equals("insert")) {
                list.insert(in.nextInt(), in.nextInt());
            } else if (op.equals("delete")) {
                list.remove(in.nextInt());
            }
        }
        list.show();
    }
}

全部评论

相关推荐

昨天 13:54
湖南大学 Web前端
秋招不是要开始了吗,我都打算润了,看大家还在找不敢润了
一条从:因为不是人人都像佬一样有实习像我们这种二本仔秋招没有实习也是白忙活
点赞 评论 收藏
分享
一表renzha:你点进去没打招呼他也会有提示的,之前我点进美的,还没打招呼,他马上给我发了不太合适哦
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务