2

当用户在链接列表中输入单个字符时,程序应该打印出列表,但是当输入字符时我遇到了问题,它不会打印出字符并导致无限循环,但在输入数字时可以完美运行。有任何想法吗?

        #include <stdio.h>  
        #include "stdafx.h"
        #include <stdlib.h>
        #include <malloc.h>

        /*Structure containing a Data part & a Link part to the next node in the List  */

        struct Node  
        {  
            int Data;  
            struct Node *Next;  
        }*Head;  



        int count()  
        {  
        /* Counting number of elements in the List*/
          struct Node *cur_ptr;  
          int count=0;  

          cur_ptr=Head;  

          while(cur_ptr != NULL)  
          {  
             cur_ptr=cur_ptr->Next;  
             count++;  
          }  
          return(count);  
        }  


        void addEnd(char input)  
         {  
            struct Node *temp1, *temp2;  

            temp1=(struct Node *)malloc(sizeof(struct Node));  
            temp1->Data=input;  

            // Copying the Head location into another node.  
            temp2=Head;  

            if(Head == NULL)  
            {  
               // If List is empty we create First Node.  
               Head=temp1;  
               Head->Next=NULL;  
            }  
            else  
            {  
               // Traverse down to end of the list.  
               while(temp2->Next != NULL)  
               temp2=temp2->Next;  

               // Append at the end of the list.  
               temp1->Next=NULL;  
               temp2->Next=temp1;  
            }  
         }    

        // Displaying list contents  

        void display()  
        {  
          struct Node *cur_ptr;  

          cur_ptr=Head;  

          if(cur_ptr==NULL)  
          {  
             printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
             printf("\nList is Empty "); 
             printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n\n");
          }  
          else  
          {  
              printf("\nElements in the List:\n\n ");  
              //traverse the entire linked list  
              while(cur_ptr!=NULL)  
              {  
                  printf(" \n-> %d ",cur_ptr->Data);  
                  cur_ptr=cur_ptr->Next;  
              }  
              printf("\n");
            }
        }

        int main(int argc, char *argv[])  
        {  
         int i=0;  

         //Set HEAD as NULL  
         Head=NULL;  

         while(1)  
         {  

            printf("\n\n\n\n\n MENU\n");  
            printf("---------------------------------\n");  
            printf(" \n1. Insert one part of DNA sequence");   
            printf(" \n2. Print the Elements in the List");   
            printf(" \n\n3. Exit\n");  
            printf(" \nChoose Option: ");  
            scanf("%d",&i);  

            switch(i)  
            {  
              case 1:  
              {  
                  char dnaChar;  
                  printf(" \nEnter char to be inserted into the List i.e A, T, G, C: ");  
                  scanf("%d",&dnaChar);  
                  addEnd(dnaChar);  
                  display();  
                  break;  
              }     

              case 2:  
              {  
                  display();  
                  break;  
              }  


              case 3:  
              {  
                  struct Node *temp;  

                  while(Head!=NULL)  
                  {  
                      temp = Head->Next;  
                      free(Head);  
                      Head=temp;  
                  }  
                  exit(0); 
                }  

              default:  
              {  
                  printf("\nWrong Option \n\n\n\n");  
              }  
            } 
         } 
        }  
4

2 回答 2

2

change scanf("%d",&dnaChar) to scanf("%c",&dnaChar) since dnaChar is a char type.

And it will start working for characters

于 2012-11-27T12:38:13.877 回答
2

您的数据类型非常不一致:

    struct Node  
    {  
        int Data;  // a "Node's Data is an int
        ...

然后在main()

              char dnaChar;   // You say you want a char
              printf(" \nEnter char to be inserted into the List i.e A, T, G, C: ");  
              scanf("%d",&dnaChar);  // then scanf using the int type %d

当您打印列表时:

              printf(" \n-> %d ",cur_ptr->Data);  // You're printing int type

所以你有一个问题,不一致。您需要为您的数据类型选择一个字符或一个 int。改变:

scanf("%d",&dnaChar);  

scanf("%c",&dnaChar);  

将修复无限循环,现在您的数据将显示为 ASCII 值:

A => 65
T => 84
G => 71
C => 67

或者您可以将所有内容更改为char/ %c,您的数据将显示为A/ T/ G/ CIMO 更易于阅读。

最后一点:

当您切换到scanf("%c",&dnaChar);您的代码时,将以不同的方式中断。scanf输入菜单选项时不会使用换行符。所以你需要这样做,否则你会直接跳过 ATGC 条目:

printf("\n\n\n\n\n MENU\n");  
printf("---------------------------------\n");  
printf(" \n1. Insert one part of DNA sequence");   
printf(" \n2. Print the Elements in the List");   
printf(" \n\n3. Exit\n");  
printf(" \nChoose Option: ");  
scanf("%d",&i);  
getchar();  // <-- Add this to get rid of newline
于 2012-11-27T12:50:07.467 回答