C 链接列表节点与类
C++ Linked List Node with class
具体来说,这里的目标是创建一个具有一定数量的节点的链接结构,在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
相关文章:
- 删除链接列表中剩余的最后一个节点
- 只有我的"链接列表"中的第一个节点正在打印
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- 创建附加到每个节点的节点列表 (c++)
- 如何仅在 2 个节点之间获取最短路径,给定邻接列表有向图?
- 如何使用LLVM C++API向保留节点列表添加
- 如何确保我的节点被添加到链接列表中
- Node中的子节点列表在C++中的BFS遍历过程中丢失
- 无法更新属于列表中节点的对象中的值
- 测试列表中节点的删除
- 如何获取列表中有子项的树的节点总和?
- 如何使用 std::list 作为公开节点的列表的接口的实现?
- 在由邻接列表表示的树中查找节点到另一个给定节点之间的路径
- C++ 链表合并排序的实现在连接 1 个以上节点的子列表时失败
- 链接列表中的所有节点都相同,似乎插入不起作用
- C 中的学生指针节点列表中的内存泄漏
- 链接节点列表,分段故障
- 有效删除N- Ary树的节点列表
- 使用 Python API 获取 Maya 中的引用节点列表
- 创建模板树节点列表时出错