带类的 C++ 链表节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22314430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Linked List Node with class
提问by user3404082
Specifically, the goal here is to create a linked structure that has some number of nodes, between 5 and 2 million. Don't worry that this number is large or that values may wrap around past the max size of integer. If you have created your linked structure correctly, a modern computer can breeze through this code very quickly. Notice that the comments describe exactly how this main should work. Here are the highlights:
具体来说,这里的目标是创建一个链接结构,该结构具有一定数量的节点,介于 5 到 200 万之间。不要担心这个数字很大或者值可能会超过整数的最大大小。如果您正确创建了链接结构,现代计算机可以非常快速地浏览此代码。请注意,注释准确地描述了这个 main 应该如何工作。以下是重点:
Create three loops The first loop creates the linked structure, hooking together the “next” fields of each node and giving each node an integer value between 0 and the randomly chosen size. The second loop adds up all of the nodes and counts them. Counting the nodes in this case should be used only as check to make sure you are not missing one. The third loop traverses all nodes again, this time deleting them.
创建三个循环 第一个循环创建链接结构,将每个节点的“下一个”字段连接在一起,并为每个节点提供一个介于 0 和随机选择的大小之间的整数值。第二个循环将所有节点相加并计算它们。在这种情况下计算节点应该仅用作检查以确保您没有遗漏一个。第三个循环再次遍历所有节点,这次删除它们。
Node.h
节点.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
节点.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;
}
It is printing out the total size then... I am getting a Seg fault:11. I am also missing some parts in the main, I am confused on how to write these as well.
它正在打印总大小然后...我收到一个 Seg 错误:11。我还缺少主要的一些部分,我也对如何编写这些部分感到困惑。
回答by michaeltang
it should be next = newNext;
instead of next = newNext->next;
它应该 next = newNext;
代替 next = newNext->next;
void Node::setNext(Node* newNext){
if(newNext == NULL)
next = NULL;
else
next = newNext;
}