看看链表输出哪里错了。
程序代码:#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#define HEADER1 " ------------------------------------图书信息-------------------------------\n"
#define HEADER2 "| 编号 | 书名 | 作者 | 类型 | 借出时间 | 借阅人 |\n"
#define HEADER3 "|------------|--------|--------|--------|----------------|-----------|\n"
#define FORMAT "| %d | %s | %s | %s | %s | %s |\n"
#define END " ----------------------------------------------------------------------------\n"
#define DATA p->code,p->name,p->author,p->type,p->time,p->UserID
struct Book
{
int code; //书本编号(唯一性)
char name[20]; //书本名称
char author[15]; //书本作者
char type[15]; //书本类型
char *time; //书本借出时间
char UserID[10]; //借阅该书的人的借书证号
struct Book *next;
};
struct Book *head_book=NULL; //图书信息链表头指针
void DataPrint()//输出图书信息链表
{
struct Book *p;
p=head_book;
printf(HEADER1);
printf(HEADER2);
if(head_book!=NULL){
do
{
printf(FORMAT,DATA);
p=p->next;
}while(p!=NULL);
printf(END);
}
else printf("没有图书信息!");
}
void BookAdd()//建立图书信息链表
{
struct Book *p1,*p2;
if(head_book==NULL)
{
printf("当前没有图书信息!,按任意键开始录入!");
getch();
system("cls");
printf(HEADER1);
head_book=p1=p2=(struct Book *)malloc(sizeof(struct Book));
printf("编号:");
scanf("%d",&p1->code);
getchar();
printf("书名:");
gets(p1->name);
printf("作者:");
gets(p1->author);
printf("类型:");
gets(p1->type);
while(p1->code!=0)
{
p2->next=p1;
p2=p1;
p1=(struct Book *)malloc(sizeof(struct Book));
printf("编号:");
scanf("%d",&p1->code);
getchar();
printf("书名:");
gets(p1->name);
printf("作者:");
gets(p1->author);
printf("类型:");
gets(p1->type);
}
p2->next=NULL;
}
else
{
p1=head_book;
while(p1->next!=NULL)
p1=p1->next;
p2=p1;
p1=(struct Book *)malloc(sizeof(struct Book));
printf("编号:");
scanf("%d",&p1->code);
getchar();
printf("书名:");
gets(p1->name);
printf("作者:");
gets(p1->author);
printf("类型:");
gets(p1->type);
while(p1->code!=0)
{
p2->next=p1;
p2=p1;
p1=(struct Book *)malloc(sizeof(struct Book));
printf("编号:");
scanf("%d",&p1->code);
getchar();
printf("书名:");
gets(p1->name);
printf("作者:");
gets(p1->author);
printf("类型:");
gets(p1->type);
}
p2->next=NULL;
}
}
void main()
{
BookAdd();
DataPrint();
}






