ユーザーがリンクリストに単一の文字を入力すると、プログラムはリストを出力する必要がありますが、文字を入力すると問題が発生し、文字が出力されず、無限ループが発生しますが、数値が入力されると完全に機能します。何か案は?
#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");
}
}
}
}