尾插法怎么把我第一个输入的书籍信息吞了呢
#include<stdio.h>
#include<stdlib.h>
struct Book{
char title[128];
struct Book* next;
};
void addBook(struct Book** lbr);
void getInput(struct Book* book);
void printlbr(struct Book* lbr);
void release(struct Book** lbr);
void addBook(struct Book** lbr)
{
struct Book* book, * temp;
book = (struct Book*)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("内存分配失败\n");
exit(1);
}
book->next = NULL;
getInput(book);
if ((*lbr) != NULL)
{
while((*lbr)->next != NULL)
{
*lbr = (*lbr)->next;
}
book->next = NULL;
(*lbr)->next = book;
}
else
{
*lbr = book;
}
}
void getInput(struct Book* book)
{
printf("请输入书名:");
scanf("%s", book->title);
}
void printlbr(struct Book* lbr)
{
struct Book* book;
int count = 1;
book = lbr;
while (book != NULL)
{
printf("Book%d:\n", count);
printf("书名: %s\n", book->title);
book = book->next;
count++;
}
}
void release(struct Book** lbr)
{
struct Book* temp;
while ((*lbr) != NULL)
{
temp = *lbr;
*lbr = (*lbr)->next;
free(temp);
}
}
int main(void)
{
struct Book* lbr = NULL;
int ch;
while (1)
{
printf("请问是否需要录入书籍信息(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
addBook(&lbr);
}
else
{
break;
}
}
printf("请问是否需要打印书籍信息(Y/N):");
do
{
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
printlbr(lbr);
}
release(&lbr);
return 0;
}
*lbr = (*lbr)->next;
这里不能改
应该另外弄一个变量
void addBook(struct Book** lbr)
{
struct Book* book, * temp;
book = (struct Book*)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("内存分配失败\n");
exit(1);
}
book->next = NULL;
getInput(book);
if ((*lbr) != NULL)
{
temp=*lbr;
while(temp->next != NULL)
{
temp = temp->next;
}
book->next = NULL;
temp->next = book;
}
else
{
*lbr = book;
}
}
【相关推荐】
int fun(int m,int n)
{
int a,b,c; a=m; b=n; c=a%b; while(c!=0) {a=b;b=c;c=a%b;} return m*n/b;
}
void main()
{
int m,n,i,t;
printf(“Enter m,n :\n”);
scanf(“%d,%d”,&m,&n);
if(m>n) { t=m; m=n; n=t; }
printf(“The Lowest Common Multiple Of %d and %d is %d\n”,m,n,fun(m,n));
}“`