[求帮助]输出链表总是开头两个出现乱码

C语言 码拜 8年前 (2016-04-13) 1198次浏览
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Equip   //定义结构体
{
char equipCode[10];    //编号
char equipType[20];    //种类
char equipName[20];    //名称
float  equipPrice;     //价格
char buyDate[20];      //购买日期
int   scrap;           //能否报废
char  scrapDate[20];  //报废日期
struct Equip *next;
}
Equip;
int main()        /*添加模块*/
{
int c;
Equip *Head,*p;
char st[]=”No”;
Head=(struct Equip*)malloc(sizeof(struct Equip));
Head->next=NULL;
printf(“**********************************************************\n”);
printf(“添加设备:\n”);
printf(“**********************************************************\n”);
while(1)
{
p=(struct Equip*)malloc(sizeof(struct Equip));
printf(“请输入设备编号\n”);
scanf(“%s”,&(p->equipCode));
printf(“请输入设备种类\n”);
scanf(“%s”,&(p->equipType));
printf(“请输入设备名称\n”);
scanf(“%s”,&(p->equipName));
printf(“请输入设备价格\n”);
scanf(“%f”,&(p->equipPrice));
printf(“输入购买日期\n”);
scanf(“%s”,&(p->buyDate));
printf(“能否报废?0/1\n”);
scanf(“%d”,&(p->scrap));
if(p->scrap==1)
{
printf(“请输入报废日期\n”);
scanf(“%s”,&(p->scrapDate));
}
else
strcpy(p->scrapDate,st);
p->next=Head->next;
Head->next=p;
printf(“还想继续输入吗?\n”);
printf(“1.是\n”);
printf(“2.不是\n”);
scanf(“%d”,&c);
if(c==2)
{
free(p);
break;
}
}
p=Head->next;
printf(“编号\t种类\t名称\t价格\t\t购买日期\t能否报废\t报废日期\n”);
while(p!=NULL)
{
printf(“%s\t%s\t%s\t%f\t%s\t\t%d\t\t%s\n”,p->equipCode,p->equipType,p->equipName,p->equipPrice,p->buyDate,p->scrap,p->scrapDate);
p=p->next;
}
}
[求帮助]输出链表总是开头两个出现乱码
不知道为什么总是最开始两个显示乱码   是哪里出错了呢
还有假如解决了写入文件再读取的话能否显示正常呢?
解决方案

20

if(c==2)
{
/*free(p);	*/
break;
}

40

都被你free了,输出自然不对了

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明[求帮助]输出链表总是开头两个出现乱码
喜欢 (0)
[1034331897@qq.com]
分享 (0)