无法正常输出图书名字
#include <iostream>
#include <stdlib.h>
typedef int ElemType;
typedef struct Node
{
ElemType ISBN;
char name[10];
char writer[10];
char location[10];
float price;
struct Node*next;
}book;
void InitList(book *&L)
{
L=(book *)malloc(sizeof(book));
if(L==NULL)
{
printf("内存分配失败!!\n");
exit(0);
}
L->next==NULL;
}
void creatList(book *&L,ElemType n)//n为要插入图书的数量
{
int i;
book *s,*r;
r=L;//r为为指针;
for(i=0;i<n;i++)
{
s=(book *)malloc(sizeof(book));
printf("请输入ISBN号:\n");
scanf("%d",&(s->ISBN));
printf("请输入书名:\n");
scanf("%s",s->name);
printf("请输入出版社:\n");
scanf("%s",s->location);
printf("请输入作者名:\n");
scanf("%s",s->name);
printf("请输入价格:\n");
scanf("%f",&s->price);
r->next=s;
r=s;
}
r->next=NULL;
}
void printfbooklist(book *&L)
{
book *pmove=L->next;
printf("ISBN 出版社 书名 作者名 价格 \n");
while(pmove)
{
printf("%5d %s %s %s %5f\n",pmove->ISBN,pmove->location,pmove->name,pmove->writer,pmove->price);
pmove=pmove->next;
}
}
int main()
{
int k,j;
book *L;
InitList(L);
printf("请输入要插入的图书数量:\n");
scanf("%d",&k);
creatList(L,k);
printfbooklist(L);
}
修改处见注释,供参考:
#include <iostream>
#include <stdlib.h>
typedef int ElemType;
typedef struct Node
{
ElemType ISBN;
char name[128]; //修改
char writer[32]; //修改
char location[128];//修改
float price;
struct Node* next;
}book;
void InitList(book*& L)
{
L = (book*)malloc(sizeof(book));
if (L == NULL)
{
printf("内存分配失败!!\n");
exit(0);
}
L->next == NULL;
}
void creatList(book*& L, ElemType n)//n为要插入图书的数量
{
int i;
book* s, * r;
r = L;//r为为指针;
for (i = 0; i < n; i++)
{
s = (book*)malloc(sizeof(book));
printf("请输入ISBN号:\n");
scanf("%d", &(s->ISBN));
getchar(); //修改
printf("请输入书名:\n");
scanf("%s", s->name);
getchar(); //修改
printf("请输入出版社:\n");
scanf("%s", s->location);
getchar(); //修改
printf("请输入作者名:\n");
scanf("%s", s->writer); //scanf("%s", s->name);//修改
getchar(); //修改
printf("请输入价格:\n");
scanf("%f", &s->price);
getchar(); //修改
r->next = s;
r = s;
}
r->next = NULL;
}
void printfbooklist(book*& L)
{
book* pmove = L->next;
printf("ISBN 出版社 书名 作者名 价格 \n");
while (pmove)
{
printf("%5d %s %s %s %5f\n", pmove->ISBN, pmove->location, pmove->name, pmove->writer, pmove->price);
pmove = pmove->next;
}
}
int main()
{
int k, j;
book* L;
InitList(L);
printf("请输入要插入的图书数量:\n");
scanf("%d", &k);
creatList(L, k);
printfbooklist(L);
return 0;
}