您好我是一名初学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);
}
}
答案 0 :(得分:1)
您的程序只是“永远”运行,因为您没有递增nextvalue
,导致while (nextvalue < 5)
在每次迭代时运行。