如何从单个链表中打印字符?

时间:2012-11-27 12:32:18

标签: c

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

        #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 个答案:

答案 0 :(得分:2)

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

它将开始为角色工作

答案 1 :(得分: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 / {{1哪个IMO更容易阅读。

最后一点:

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

scanf