为什么当我制作一个链表时,我的电脑会崩溃
Why does my computer crash when I make a linked list?
大家好,我是一名初级c++程序员,一直在考虑链表。我想出了以下代码,但当我运行它时,我的电脑崩溃了。我想知道到底是什么原因导致我的电脑出现这种情况。
#include <iostream>
using namespace std;
struct list
{
int value;
list* nextlist;
};
list* getnewstruct (list* phead, int nextvalue);
void printarray (list* phead);
int main()
{
int nextvalue = 0;
list* phead = NULL;
while (nextvalue < 5) {
phead = getnewstruct (phead, nextvalue);
}
printarray (phead);
}
list* getnewstruct (list* phead, int nextvalue)
{
list* newlist = new list;
newlist->value = nextvalue;
newlist->nextlist = phead;
return newlist;
}
void printarray (list* phead)
{
while (phead->nextlist != NULL) {
cout<<phead->value<<endl;
printarray (phead->nextlist);
}
}
您的程序只是"永远"运行,因为您没有递增nextvalue
,这会导致while (nextvalue < 5)
在每次迭代中运行。
相关文章:
- 反向给定链表中的K节点
- C++,指针数组,指向双链表中的条目
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 链表中的显示功能无法正常工作
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 链表中的 C++ 析构函数
- 遍历链表时的无限循环
- 在链表中的第 n 位插入显示分割错误
- 我如何能够访问双向链表中的嵌套结构
- 为什么我的函数没有打印出双向链表中的第一个节点?
- 我在双链表上的此打印功能时遇到问题
- 如何将节点添加到链表C++的前面?我想多了这个概念
- C++我的第一个链表我做错了什么
- 对象数组作为链表中的字符数组 - 我对对齐的假设是否有效
- 我如何使用C ++以相反的顺序打印此单个链表中的元素
- 我不能删除双向链表中的最小数字,然后删除下一个
- 我的双向链表中的查找函数损坏
- 我将如何在不损坏列表的情况下删除链表中的节点
- 我写了这个程序来反转链表中的元素,在编译这个程序后,在reverse()中显示错误.为什么