首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

大神帮小弟我看看有关于链表的程序吧

2013-01-01 
大神帮我看看有关于链表的程序吧我本来要写一个程序链表已经写好了,里面的操作还么有加上在调试的时候出现

大神帮我看看有关于链表的程序吧
我本来要写一个程序链表已经写好了,里面的操作还么有加上
在调试的时候出现问题  
因为我的程序用的是死循环,所以我想通过每次循环的时候用输入y或n来决定循环的结束是否
但是这里的程序只在第一次的时候判断了是否继续后面的直接跳过了这是怎么会事
随便我是新手帮我科普一下知识大神帮小弟我看看有关于链表的程序吧
下面是代码

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
 
int main(void)
{
struct zhifu
{
int no;
char chuan[80];
struct zhifu *next;
};
struct zhifu *first = NULL;
struct zhifu *ing = NULL;
struct zhifu *previous = NULL;
char test='\0';
for(;;)
{
printf("\n你想继续输入字符数组吗(Y or N)?",first != NULL?"nother":"");
scanf("%c",&test);
if(tolower(test)=='n')
break;

ing=(struct zhifu*)malloc(sizeof(struct zhifu));

if(first==NULL)
first =ing;

if(previous!=NULL)
previous->next=ing;
printf("\n这是第几次定义字符数组:");
scanf("%d",&ing->no);
printf("\n请定义第%d次字符数组:",ing->no);
scanf("%s",ing->chuan);

ing->next=NULL;
previous=ing;
}
ing=first;

while(ing!=NULL)
{






}




return 0;
}

[解决办法]
[code=c]
for(;;)
{
    printf("\n你想继续输入字符数组吗(Y or N)?",first != NULL?"nother":"");
    scanf("%c",&test);
    if(tolower(test)=='n')
        break;
     ing=(struct zhifu*)malloc(sizeof(struct zhifu));
     if(first==NULL)
        first =ing;
     if(previous!=NULL)
        previous->next=ing;
    printf("\n这是第几次定义字符数组:");
    scanf("%d",&ing->no);
    printf("\n请定义第%d次字符数组:",ing->no);
    scanf("%s",ing->chuan);
     ing->next=NULL;
    previous=ing;
    fflush(stdin) //<--加上这句
}
ing=first;
[解决办法]

 for(;;)
 {
     printf("\n你想继续输入字符数组吗(Y or N)?",first != NULL?"nother":"");
     scanf("%c",&test);
     if(tolower(test)=='n')
         break;
      ing=(struct zhifu*)malloc(sizeof(struct zhifu));
      if(first==NULL)
         first =ing;
      if(previous!=NULL)
         previous->next=ing;
     printf("\n这是第几次定义字符数组:");
     scanf("%d",&ing->no);
     printf("\n请定义第%d次字符数组:",ing->no);
     scanf("%s",ing->chuan);
      ing->next=NULL;
     previous=ing;
     fflush(stdin) //<--加上这句
 }

[解决办法]
 


 for(;;)
 {
     printf("\n你想继续输入字符数组吗(Y or N)?",first != NULL?"nother":"");
     scanf("%c",&test);
     if(tolower(test)=='n')
         break;
      ing=(struct zhifu*)malloc(sizeof(struct zhifu));
      if(first==NULL)
         first =ing;
      if(previous!=NULL)
         previous->next=ing;
     printf("\n这是第几次定义字符数组:");
     scanf("%d",&ing->no);
     printf("\n请定义第%d次字符数组:",ing->no);
     scanf("%s",ing->chuan);
      ing->next=NULL;
     previous=ing;
     fflush(stdin); //<--加上这句
 }

热点排行