【问题标题】:infinite loop after the user input用户输入后的无限循环
【发布时间】:2013-07-20 03:41:28
【问题描述】:

我们的教授询问了使用链表和指针来执行 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(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

对不起,我是新手。似乎是什么问题?谢谢。

【问题讨论】:

    标签: c++ pointers linked-list nodes infinite-loop


    【解决方案1】:

    不错的代码,试试这个

    在 void makenull(list *head) 函数中,你有一个无限循环,因为你没有 释放头部的链接。所以它显然是一个无限循环。 所以试试这个...

       void makenull(list *head)
       {
    
       (*head)->value = NULL;//make data empty
    
       (*head)->link = NULL;//make link empty
    
        print(head);
    
      }//makenull 
    

    希望这行得通。

    【讨论】:

    • 他从不分配头节点,也从不处理其他函数中的头节点。
    【解决方案2】:
    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
    

    当你进入程序时,你必须先 makenull 以便 head 为 NULL。但请参阅添加代码。如果 head 为 NULL,则您实际上从未将 head 指针设置为有效节点……这就是为什么您在添加到尾部时崩溃的原因。我假设其他路径也有类似的问题。内存从未正确初始化。

    【讨论】:

    • 我运行程序并在 cout 之后>选择;它崩溃了。是的,我知道。感谢有关头的提示,我是这类东西的新手。
    • 好吧,这与无限循环有很大不同。在一个新程序中这样做:while(true) {} 看看会发生什么……那是一个无限循环。所以你在 cin >> 选择上崩溃了;我记得 cin 和 chars 的一些有趣的东西让我做一些研究
    • 用户输入后它仍然崩溃。即使在其他功能中。它仍然崩溃。不知道是什么问题。
    • 第三个选项?问题是您不能将 cin >> 转换为字符。您可以将 cin 转换为 char * 或字符串...如果您输入一个数字,它可能对 char 有效...最好还是切换到数字,除非它需要使用 char,否则它会更干净。
    • 哦!老兄!你什么时候分配头的?因此,在您的添加功能中,您取消引用未设置指针上的链接。您必须在您的主要设置 *head 中为 null,并且在您的添加和删除函数中考虑到它们可能正在添加或删除头部的事实
    猜你喜欢
    • 1970-01-01
    • 2021-03-12
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2012-10-18
    相关资源
    最近更新 更多