C++ 代码在运行时挂起
代码在执行时挂起。请帮助我了解这里的问题是什么 它在第一次遍历后进行STCK,甚至不进入反向功能。无法理解此问题: 帮我吧C++ 代码在运行时挂起,c++,C++,代码在执行时挂起。请帮助我了解这里的问题是什么 它在第一次遍历后进行STCK,甚至不进入反向功能。无法理解此问题: 帮我吧 # include<iostream> # include<stdlib.h> using namespace std; struct sLL { int data; sLL * next; }; void createList(sLL ** head, int n) { if(n == 0) { retur
# include<iostream>
# include<stdlib.h>
using namespace std;
struct sLL
{
int data;
sLL * next;
};
void createList(sLL ** head, int n)
{
if(n == 0)
{
return;
}
sLL * temp = (sLL *) malloc(sizeof(sLL));
if(temp != NULL)
{
temp->data = rand() % 100;
temp->next = NULL;
*head = temp;
//head = &(temp->next);
createList(&(temp->next), n - 1);
}
}
void traverse(sLL * head)
{
cout<<"In traverse"<<endl;
while(head) {
cout<<head->data<<"->";
head=head->next;
}
cout<<"NULL"<<endl;
cout.flush();
return;
}
sLL* reverse(sLL * head)
{
sLL * temp = NULL, * newNode = NULL;
while(head) {
newNode = head->next; // to traverse forword
head->next = temp;
temp = head; // Current node value which we use in next itr as a previous node value.
newNode = head; // assigning newNode vlaue back to head so that we can traverse forward.
}
return temp; // final node value will be in temp i.e the current node value.
}
int main()
{
int n;
sLL * head = NULL, *temp = NULL;
cout<<"Enter the number of nodes :";
cin>>n;
createList(&head, n);
cout<<"\nCreate Done"<<endl;
traverse(head);
cout.flush();
cout<<"Reverse Start";
temp = reverse(head);
cout<<"reverse done";
traverse(temp);
}
您根本不需要重新分配头值
while(head)
//loop
在这里,头指针永远不会改变。因此循环是无限的。您的评论与此处的代码相矛盾:
newNode = head; // assigning newNode vlaue back to head so that we can traverse forward.
如果你按照评论所说的去做,应该会更好:
head = newNode; // assigning newNode value back to head so that we can traverse forward.
您能指出横移函数SIR head=head->next的while循环中的@saurabh的位置吗;是否存在…@saurabh抱歉,反向函数不是无限循环。。。。。在我旅行的同时,旅行也进行得很好,超过了打印的值吗?通过有选择地注释,直到只剩下很少的行,缩小问题的范围。这是呼叫调试,可能会为您解决问题…明白了,谢谢aswin已经解决了