C 链接列表节点与类

C++ Linked List Node with class

本文关键字:节点 列表 链接      更新时间:2023-10-16

具体来说,这里的目标是创建一个具有一定数量的节点的链接结构,在5到200万之间。不用担心这个数字很大,或者值可能会超过整数的最大大小。如果您正确地创建了链接结构,那么现代计算机可以很快通过此代码轻而易举。请注意,评论准确地描述了该主要的工作原理。这是亮点:

创建三个循环第一个循环创建了链接的结构,将每个节点的"下一个"字段钩在一起,并使每个节点在0和随机选择的大小之间一个整数值。第二个循环添加了所有节点并对其进行计数。在这种情况下,计算节点应仅作为检查,以确保您不会丢失一个。第三个循环再次遍历所有节点,这次将它们删除。

node.h

class Node {
public:
    Node();
    Node(const Node& orig);
    virtual ~Node();
    bool hasNext();
    Node* getNext();
    void setNext(Node* newNext);
    int getValue();
    void setValue(int val);
private:
    Node* next;
    int value;
};
#endif

node.cpp

include "Node.h"
include <iostream>
Node::Node() {
    next = NULL;
}
Node::Node(const Node& orig) {
    next = orig.next;
    value = orig.value;
}
Node::~Node() {
}
bool Node::hasNext(){
    if (next != NULL)
        return true;
    else
        return false;
}
Node* Node::getNext(){
    return next;
}
void Node::setNext(Node* newNext){
    if(newNext == NULL)
        next = NULL;
    else
        next = newNext->next;
}
int Node::getValue(){
    return value;
}
void Node::setValue(int val){
    value = val;
}

main.cpp

include <cstdlib>
include <iostream>
include "Node.h"
include <time.h>
using namespace std;
int main(int argc, char** argv) {
    //This is the node that starts it all
    Node *tail;
    Node* head = new Node();
    //select a random number between 5 and 2,000,000
    srand(time(NULL));
    int size = (rand() % 2000000) + 5;
    int total = 0;
    int counter = 0;
    //print out the size of the list that will be created/destroyed
    cout << "The total size is: " << size << endl;
    head->setValue(0);
    tail = head;
    Node *newNode = new Node;
    for (int i = 1; i < size; i++){  
        Node *newNode = new Node;
        newNode->setValue(i);
        newNode->setNext(NULL);
        tail->setNext(newNode);
        tail = newNode;
    }
    //Create a list that counts from 0 to 2,000,000
    //Link all of the nodes together
    //A for loop is easiest here
    cout << head->getNext()->getValue();
    Node* current = head;
    while (current != NULL){ 
        counter += current->getValue();
        cout << current->getValue();
        current = current->getNext();
        total++;
    }
    //Traverse the list you created and add up all of the values
    //Use a while loop
    //output the number of nodes. In addition, print out the sum
    //of all of the values of the nodes.
    cout << "Tracked " << total << " nodes, with a total count of " << counter << endl;
    //Now loop through your linked structure a third time and
    //delete all of the nodes
    //Again, I require you use a while loop
    cout << "Deleted " << total << " nodes. We're done!" << endl;
    return 0;
}

它正在打印出总尺寸,然后...我遇到了SEG错误:11。我也缺少一些部分,我对如何编写这些部分感到困惑。

应该是 next = newNext;而不是 next = newNext->next;

void Node::setNext(Node* newNext){
    if(newNext == NULL)
        next = NULL;
    else
        next = newNext;
}

首先,避免在抽象数据类型中使用getter函数。这些应该保留给您的客户测试应用程序;将它们放在您的ADT之外。相反,将任何值传递为参数为re:原型。其次,避免在您的课堂中使用无效的返回方法。而是返回bool或int。零或错误的错误,true或某些非零整数用于您的成功消息。

除此之外,我正在寻找使用课程来构建节点的方法,您的帖子出现了。有趣的开始。我们将看到我们的去向。

ciao,lewsutt