链表内元素的个数

发布于 2025-01-10 04:59:24 字数 3397 浏览 0 评论 0原文

我想保存链表类对象实例内的元素数量。从下面的代码来看,每次我调用 addNodeFront()addNodeBack() 函数时,成员变量 len 应该增加 1。但是,当我运行代码,getLen函数只返回1,而链表有2个元素。我应该从我的代码中修复什么?

#include <iostream>

class Node {
    public:
        int value;
        Node* next;
        
        Node(int value, Node* next) {
            this->value = value;
            this->next = next;
        }
};

class LinkedList {
public:
    Node* head;
    Node* tail;
    int len = 0;
    
    LinkedList() {
        this->head = nullptr;
        this->tail = nullptr;
    }
    
    LinkedList(Node* node) {
        this->head = node;
        this->tail = node;
    }

    int getLen() {
        return len;
    }
    
    void addNodeFront(Node* node) {
        if(head==nullptr && tail==nullptr) {
            this->head = node;
            this->tail = node;
            return;
        }
        Node* secondFirst = this->head;
        this->head = node;
        node->next = secondFirst;
        this->len++;
    }
    
    void addNodeBack(Node* node) {
        if(head==nullptr && tail==nullptr) {
            this->head = node;
            this->tail = node;
            return;
        }
        this->tail->next = node;
        this->tail = node;
        this->len++;
    }
    
    void addNodeAfterNode(Node* prevNode, Node* node) {
        if(prevNode == this->tail) {
            this->tail = node;
            prevNode->next = node;
            return;
        }
        node->next = prevNode->next;
        prevNode->next = node;
    }
    
    bool searchVal(int val) const {
        Node* s = this->head;
        while(s != nullptr) {
            if(s->value == val) return true;
            s = s->next;
        }
        return false;
    }
    
    void deleteNodeFront() {
        if(this->head==this->tail) {
            this->head = nullptr;
            this->tail = nullptr;
            return;
        }
        this->head = this->head->next;
    }

    void deleteNodeBack() {
        Node* secondLast = this->head;
        if(this->head==this->tail) {
            this->head = nullptr;
            this->tail = nullptr;
            return;
        }
        while(secondLast->next != this->tail) {
            secondLast = secondLast->next;
        }
        secondLast->next = nullptr;
        this->tail = secondLast;
    }

    void deleteNodeMiddle(Node* node) {
        if(node==this->head || node==this->tail) return;
        Node* prevNode = this->head;
        while(prevNode->next != node) {
            prevNode = prevNode->next;
        }
        prevNode->next = prevNode->next->next;
    }
    
    void traverseLinkedList() {
        Node* t = this->head;
        if(head==nullptr && tail==nullptr) std::cout << "Empty Linked List";
        while(t != nullptr) {
            std::cout << t->value << "->";
            t = t->next;
        }
        std::cout << "\n";
    }

};

int main() {
    Node node1(2,nullptr);
    Node node4(4,nullptr);
    LinkedList ls;
    ls.addNodeFront(&node1);
    ls.addNodeFront(&node4);
    std::cout << ls.getLen() << std::endl;
    ls.traverseLinkedList();
}

I want to save the number of element inside an instance of linked list class object. From the code below everytime I call addNodeFront() or addNodeBack() function, member variable len should be incremented by 1. But, when I run the code, the getLen function only return 1, while the linked list has 2 element. What should I fix from the my code?

#include <iostream>

class Node {
    public:
        int value;
        Node* next;
        
        Node(int value, Node* next) {
            this->value = value;
            this->next = next;
        }
};

class LinkedList {
public:
    Node* head;
    Node* tail;
    int len = 0;
    
    LinkedList() {
        this->head = nullptr;
        this->tail = nullptr;
    }
    
    LinkedList(Node* node) {
        this->head = node;
        this->tail = node;
    }

    int getLen() {
        return len;
    }
    
    void addNodeFront(Node* node) {
        if(head==nullptr && tail==nullptr) {
            this->head = node;
            this->tail = node;
            return;
        }
        Node* secondFirst = this->head;
        this->head = node;
        node->next = secondFirst;
        this->len++;
    }
    
    void addNodeBack(Node* node) {
        if(head==nullptr && tail==nullptr) {
            this->head = node;
            this->tail = node;
            return;
        }
        this->tail->next = node;
        this->tail = node;
        this->len++;
    }
    
    void addNodeAfterNode(Node* prevNode, Node* node) {
        if(prevNode == this->tail) {
            this->tail = node;
            prevNode->next = node;
            return;
        }
        node->next = prevNode->next;
        prevNode->next = node;
    }
    
    bool searchVal(int val) const {
        Node* s = this->head;
        while(s != nullptr) {
            if(s->value == val) return true;
            s = s->next;
        }
        return false;
    }
    
    void deleteNodeFront() {
        if(this->head==this->tail) {
            this->head = nullptr;
            this->tail = nullptr;
            return;
        }
        this->head = this->head->next;
    }

    void deleteNodeBack() {
        Node* secondLast = this->head;
        if(this->head==this->tail) {
            this->head = nullptr;
            this->tail = nullptr;
            return;
        }
        while(secondLast->next != this->tail) {
            secondLast = secondLast->next;
        }
        secondLast->next = nullptr;
        this->tail = secondLast;
    }

    void deleteNodeMiddle(Node* node) {
        if(node==this->head || node==this->tail) return;
        Node* prevNode = this->head;
        while(prevNode->next != node) {
            prevNode = prevNode->next;
        }
        prevNode->next = prevNode->next->next;
    }
    
    void traverseLinkedList() {
        Node* t = this->head;
        if(head==nullptr && tail==nullptr) std::cout << "Empty Linked List";
        while(t != nullptr) {
            std::cout << t->value << "->";
            t = t->next;
        }
        std::cout << "\n";
    }

};

int main() {
    Node node1(2,nullptr);
    Node node4(4,nullptr);
    LinkedList ls;
    ls.addNodeFront(&node1);
    ls.addNodeFront(&node4);
    std::cout << ls.getLen() << std::endl;
    ls.traverseLinkedList();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2025-01-17 04:59:24

在下面的函数中:

LinkedList(Node* node) 
{
    this->head = node;
    this->tail = node;
}

只需添加以下行之一:

len++; //1
len = 1; //2

所以更新后的函数:

LinkedList(Node* node) 
{
    this->head = node;
    this->tail = node;

    len++; //1
    len = 1; //2
}

这是因为作为 head 节点tail 节点 = node 现在,这意味着链表中有一个节点,所以我们应该给 len 加 1。

In the following function:

LinkedList(Node* node) 
{
    this->head = node;
    this->tail = node;
}

Just add one of the following lines:

len++; //1
len = 1; //2

So the updated function:

LinkedList(Node* node) 
{
    this->head = node;
    this->tail = node;

    len++; //1
    len = 1; //2
}

This is because as the head node and the tail node = node now, that means there is a node in the linked list, so we should add 1 to len.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文