尾插法怎么把我第一个输入的书籍信息吞了呢#include <stdio.h>

尾插法怎么把我第一个输入的书籍信息吞了呢


#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;
    }
}

【相关推荐】



  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/789133
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:标准函数库(#include)
  • 除此之外, 这篇博客: 用函数计算两个正整数的最小公倍数中的 include “stdio.h” 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

    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));
    }“`这里写图片描述


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^