• 【面试题】c++实现简易单链表


    #include 
    //实现单链表
    class Node {
    public:
        int data;
        Node* next;
        Node(int value) :data(value), next(nullptr) {}
    };
    
    class LinkedList
    {
    private:
        Node* head;
    
    public:
        LinkedList() :head(nullptr) {}
        //添加元素在最后
        void appent(int value) {
            Node* new_node = new Node(value);
            if (!head) {
                head = new_node;
                return;
            }
            Node* current = head;
            while (current->next)
            {
                current = current->next;
            }
            current->next = new_node;
        }
        //显示函数
        void display() {
            Node* current = head;
            while (current)
            {
                std::cout << current->data << "->";
                current = current->next;
            }
            std::cout << "nullptr" << std::endl;
        }
        //删除一个特定节点
        void remove(int value) {
            if (!head) {
                return;
            }
            if (head->data == value) {
                Node* temp = head;
                head = head->next;
                delete temp;
            }
            Node* current = head;
            while (current->next)
            {
                if (current->data == value) {
                    Node* temp = current->next;
                    current->next = current->next->next;
                    delete temp;
                    return;
                }
                current = current->next;
            }
        }
        //查找
        bool search(int value) { 
            Node* current = head;
            while (current) {
                if (current->data == value) {
                    return true;
                }
                current = current->next;
            }
            return false;
        }
        //清除
        void clear() {
            while (head) {
                Node* temp = head;
                head = head->next;
                delete temp;
            }
        }
        ~LinkedList() {
            clear();
        }
    };
    
    
    int main()
    {
        LinkedList Mylist;
        Mylist.appent(1);
        Mylist.appent(2);
        Mylist.appent(3);
        Mylist.appent(4);
        Mylist.display();
        Mylist.remove(1);
        Mylist.display();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
  • 相关阅读:
    生产者消费者模式(c++实现)
    vue中什么是$nextTick?
    mysql存储过程
    ETCD中MVCC的运用
    号码认证平台有哪些?号码认证平台费用?
    1.4.25 实验25:华为高级ACL
    [附源码]Java计算机毕业设计SSM动漫下载网站
    借助ChatGPT撰写学术论文,如何设定有效的角色提示词指
    vue---------商城pc端 购物车模块
    通用文档信息提取模型浅析
  • 原文地址:https://blog.csdn.net/qq_51214753/article/details/133210851