C++ 用户输入后的无限循环
我们的教授要求使用链表和指针来执行ADT列表操作。 编译时没有错误。但每个函数最终都会进入一个无限循环C++ 用户输入后的无限循环,c++,pointers,linked-list,nodes,infinite-loop,C++,Pointers,Linked List,Nodes,Infinite Loop,我们的教授要求使用链表和指针来执行ADT列表操作。 编译时没有错误。但每个函数最终都会进入一个无限循环 #include <stdlib.h> #include <iostream> using namespace std; char choice; struct node { int value; struct node *link; }; typedef struct node *list; list *head; void print(lis
#include <stdlib.h>
#include <iostream>
using namespace std;
char choice;
struct node
{
int value;
struct node *link;
};
typedef struct node *list;
list *head;
void print(list *head)
{
list ptr=NULL;
if (*head!=NULL)
{
for (ptr=*head; ptr->link!=NULL; ptr=ptr->link)
cout<< ptr->value <<" ";
cout<<ptr->value;
} //if
else
cout<<"NOTHING TO PRINT!";
} //print
void add(list *head)
{
int num;
list ptr;
cout<<"What do you want to add: ";
cin>>num;
cout<<"Options: "<<endl<<"A. Addtail"<<endl<<"B. Addhead"<<endl
<<"Choice : ";
cin>>choice;
switch(choice)
{
case 'a':
case 'A':
{
list newnode,ptr;
ptr=*head;
newnode=(list)malloc(sizeof(struct node));
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=newnode;
newnode->value=num;
newnode->link=NULL;
break;
} // case a
case 'b':
case 'B':
{
list newnode;
newnode = (list)malloc(sizeof(struct node));
newnode->value=num;
newnode->link=*head;
*head=newnode;
break;
} // case b
} // switch
print(head);
} // add
void deleted(list *head)
{
list ptr;
cout<<"Options: "<<endl<<"A. Deletetail"<<endl<<"B. Deletehead"<<endl
<<"Choice : ";
cin>>choice;
switch(choice)
{
case 'a':
case 'A':
{
list ptr,ptr2;
ptr=*head;
if(*head!=NULL)
{
while(ptr->link!=NULL)
{
ptr2=ptr;
ptr=ptr->link;
} //while
free(ptr);
ptr2->link=NULL;
}//if
else
cout<<"Nothing to delete!";
}//case a
case 'b':
case 'B':
{
list ptr;
if(*head!=NULL)
{
ptr=*head;
*head=ptr->link;
free(ptr);
}
else
cout<<"Nothing to delete!";
}//case b
}//switch
print(head);
}//delete
void empty(list *head)
{
if (*head !=NULL)
cout<<"The list is not Empty"<<endl<<endl;
else
cout<<"The List is Empty"<<endl<<endl;
}//empty
void makenull(list *head)
{
*head = NULL;
print(head);
}//makenull
main ()
{
int dota=0;
while(dota<10)
{
cout<<"ADT List Operations:"<<endl<<"A. Add"<<endl<<"B. Delete"<<endl<<
"C. Empty"<<endl<<"D. Make Null"<<endl<<"E. Print"<<endl<<"F. Exit"<<endl
<<"Choice: ";
cin>>choice;
switch(choice)
{
case 'a':
case 'A': {add(head);break;}
case 'b':
case 'B': {deleted(head);break;}
case 'c':
case 'C': {empty(head);break;}
case 'd':
case 'D': {makenull(head);break;}
case 'e':
case 'E': {print(head);break;}
case 'f':
case 'F': {dota=100;break;}
}//switch
}//while
cout<<"Do you want to try again?"<<endl<<"Choice : ";
cin>>choice;
if(choice =='Y' || choice =='y')
{makenull(head); main();}
system("pause");
}//main
#包括
#包括
使用名称空间std;
字符选择;
结构节点
{
int值;
结构节点*链接;
};
typedef结构节点*列表;
名单*标题;
作废打印(列表*标题)
{
列表ptr=NULL;
如果(*head!=NULL)
{
对于(ptr=*head;ptr->link!=NULL;ptr=ptr->link)
库特值
当你进入程序时,你必须先将head设置为NULL,这样head就为NULL。但是请看添加代码。如果head为NULL,你就不会真正将head指针设置为有效的节点……这就是为什么你会在add to tail上崩溃的原因。我假设其他路径也有类似的问题。内存从来没有正确初始化过。代码不错,试试看而是这个
在void makenull(list*head)函数中,您有一个无限循环,因为您没有
释放头部的链接,这显然是一个无限循环。
所以试试这个
void makenull(list *head)
{
(*head)->value = NULL;//make data empty
(*head)->link = NULL;//make link empty
print(head);
}//makenull
希望这能起作用。我运行了这个程序,在coutWell之后,好的,这与无限循环有很大不同。在一个新程序中,这样做:while(true){}看看会发生什么…这是一个无限循环。所以你在cin>>选择上崩溃了;我记得cin和chars有一个有趣的地方让我做一些研究。在用户输入后,它仍然崩溃。即使在其他函数中。它仍然崩溃。不知道是什么问题。即使是第三个选项?问题是你不能将cin>>转换为char。你可以将cin转换为char*或字符串…如果您输入一个数字,它可能会对char起作用…无论如何,最好只切换到数字,它会更干净,除非需要使用char。哦!伙计!您什么时候分配了头?所以在add函数中,您取消对未设置指针上的链接的引用。您必须在main set*中将头设置为null,在add和delete函数中使用i要考虑到他们可能正在添加或删除head,他从未处理head节点,也从未在其他函数中处理head节点。
void makenull(list *head)
{
(*head)->value = NULL;//make data empty
(*head)->link = NULL;//make link empty
print(head);
}//makenull