2012-11-27 38 views
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"); 
       } 
      } 
     } 
     } 
+2

'scanf(“%d”)'需要输入一个数字。尝试获取输入为字符串或字符,然后将其转换回来。 –

回答

2

变化scanf("%d",&dnaChar)scanf("%c",&dnaChar)因为dnaCharchar类型。

它将开始为角色工作

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/C哪些IM O更容易阅读。

末点:

当您切换到scanf("%c",&dnaChar);您的代码将在differen的方式打破。当您进入菜单选项时,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