简体   繁体   English

C:使用链表将数据添加到结构

[英]C: Adding data to structure using linked list

I'm new to c and learning about linked lists, I decided to create a library manager to manage my books using linked list but it doesn't seem to be saving the data to the structure using linked list. 我是c语言的新手,并且了解链表,因此我决定创建一个图书馆管理器来使用链表管理我的书,但是似乎并没有使用链表将数据保存到结构中。 When someone tries to add a new book the function hits the checkID function to see if the book with the same id already exists but when i do a display information nothing seems to exists in the structure. 当有人尝试添加一本新书时,该功能会点击checkID函数以查看是否存在具有相同ID的书,但是当我执行显示信息时,该结构中似乎就不存在任何内容。

void addBook()
{
    int bookId;
    BOOK *head = NULL, *temp;
    temp = (BOOK*) malloc(sizeof(BOOK));

    printf("\n Enter Book Details\n");
    printf("Enter book ISBN: ");
    scanf("%d", &bookId);
    int bInd = checkID(bookId);
    if (bInd == 0)
    {
        printf("Enter book title: ");
        scanf("%s", &temp->chTitle);
        printf("Enter book type (eg. magazine, novel): ");
        scanf("%s", &temp->chType);
        printf("Enter book publisher (eg. UTA): ");
        scanf("%s", &temp->chPublisher);
        printf("Enter book's number of pages: ");
        scanf("%d", &temp->nPages);
        printf("Enter book's price: ");
        scanf("%f", &temp->nPrice);
        printf("Enter year published: ");
        scanf("%d", &temp->nPubYear);
        //temp->next=NULL;
        if (head == NULL)
        {
            head = temp;
            temp->next = NULL;
        }
        else{
            temp->next = head;
            head = temp;
        }
        //BOOK[count].nStatus = IN;
        count++;
    }
    else
    {
        printf("\nSorry another book with that id: Try again!\n" );
        addBookFunction();
    }
}

int checkID(int t)
{
    BOOK *head;
    while (head != NULL)
    {
        if (head->nID == t)
            return 1;
        head = head->next;
    }
    return 0;
}

Don't worry ! 不用担心! It's normal ! 这是正常的 ! You are creating a new book library each time you add a book ! 每次添加图书时,您都在创建一个新的图书库! :) :)

At your place, an easy way to solve it would be to pass your "Library collection of book" to your AddBook method : 在您自己的地方,一种简单的解决方法是将您的“书库”传递给您的AddBook方法:

void addBook(BOOK *libraryBooks)
int checkID(BOOK *libraryBooks, int t)

Then remove your variable declaration 'head'. 然后删除变量声明“ head”。 Change all 'head' by 'libraryBooks' and pass the variable to checkID. 通过“ libraryBooks”更改所有“ head”,并将变量传递给checkID。

Above this function you'll have to manage your library. 在此功能之上,您将必须管理您的库。

checkID() and addBook() should probably both accept the head of your linked list as an additional parameter. checkID()addBook()可能都应该接受链接列表的头部作为附加参数。 The head pointer that iterates through your linked list in checkID() looks like it's getting used before it's initialized with a proper value. 遍历checkID()链表的head指针看起来像在使用适当的值初始化之前就已经被使用。 It may just be lucky happenstance that you haven't hit a null pointer exception already. 可能还很幸运,您还没有遇到空指针异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM