不知道怎么回事,各位帮忙看看
我想写一个简单链表,直接上程序吧,在注释中标明问题
程序代码:#include <stdio.h>
#include <stdlib.h>
struct node
{
char a;
struct node *next;
};
void printList(struct node *Tou)
{
struct node *h1=Tou;
while(h1->next!=NULL)
{
h1=h1->next;
printf("%c --> ",h1->a);
}
printf("NULL\n\n");
}
void insert(struct node *Tou)
{
struct node *h1=Tou,*h2=(struct node *)malloc(sizeof (struct node));
printf("Enter a character:");
// int n; //非得加上这两句,把'\n'消除了才能输入h2->a……非常不解
// scanf("%d",&n);
scanf("%c",&h2->a); //如果不加上上面两句,在运行时这句就会直接跳过,给h2->a赋的值是'\n'
h2->next=NULL;
while(h1->next!=NULL)
h1=h1->next;
h1->next=h2;
printList(Tou);
}
int main(void)
{
printf("Enter your choice:\n"
"1 to insert an element into the list.\n"
"2 to delete an element from the list.\n"
"3 to end.\n? ");
int n;
scanf("%d",&n); //我在想原因就是在这输入n时,还有一个'\n'在输入流中,影响了后面的输入,为什么会这样呢……
struct node *Tou=(struct node *)malloc(sizeof (struct node));
Tou->a=0;
Tou->next=NULL;
while(n!=3)
{
switch(n)
{
case 1:
insert(Tou);
break;
}
printf("? ");
scanf("%d",&n);
}
return 0;
}请大家看看,谢啦









