#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define N sizeof(struct equ)
struct equ
{
char num[10];
char name[20];
char model[20];
char price[10];
char date[20];
char user[20];
char remark[50];
struct equ *next;
};
struct equ *input()
{
struct equ *head;
struct equ *p1,*p2;
int n=0;
p1=p2=(struct equ *)malloc(N);
printf("Please input 'number name model price date user remark\n");
while(1)
{
n=n+1;
if ( n==1 )
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct equ *)malloc(N);
printf("Number: ");gets(p1->num);fflush(stdin);
printf("Name: ");gets(p1->name);fflush(stdin);
printf("Model: ");gets(p1->model);fflush(stdin);
printf("Price: ");gets(p1->price);fflush(stdin);
printf("Date: ");gets(p1->date);fflush(stdin);
printf("user: ");gets(p1->user);fflush(stdin);
printf("Remark: ");gets(p1->remark);fflush(stdin);
printf("end?y or n");if(getch()=='y'){clrscr();break;}
clrscr();
}
p2->next=NULL;
printf("The end\n");
return(head);
}
struct equ *output(struct equ *h)
{
struct equ *p;
p=h;
clrscr();
if(h!=NULL)
do
{puts(p->num);
puts(p->name);
puts(p->remark);
printf("\n\n");
p=p->next;
}while(p!=NULL);
return(h);
}
main()
{
struct equ *h=NULL;
h=input();
output(h);
getch();
}
head那里,多申请了一块内存,导致head指向的空间没有被赋值过,也没有初始化,输出会是乱码。