c - C 中的链接列表 - 在排序位置添加结构

标签 c linked-list singly-linked-list

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "Book.h"


int main(int argc, char** argv) {

    Book * dummy = newBook("dummy", "dummy", 00000);

    printf("%s %s %ld", dummy->title, dummy->author, dummy->ISBN);

    dummy->next = NULL;


    Book* newishBook = newBook("Foo", "Chris", 1234);
    insertToList(newishBook, dummy);

    Book* another = newBook("Bar", "Jim", 23344);
    insertToList(another, dummy);

    Book* yet = newBook("Derp", "Bob", 999);
    insertToList(yet, dummy);

    displayList(dummy);

    searchISBN(999);

    return (EXIT_SUCCESS);
}

Book* newBook(char* newTitle, char* newAuthor, long newISBN) {
    Book* new_book = malloc(sizeof(Book));

    strcpy(new_book->title, newTitle);
    strcpy(new_book->author, newAuthor);
    new_book->ISBN = newISBN;
    return new_book;
}



void insertToList(Book* bookToInsert, Book* dummy){
    Book* currentNode = dummy;
    Book* temp = malloc(sizeof(Book));

    if (currentNode->next == NULL){
        currentNode->next = bookToInsert;
        printf("Head");
    } else {
        currentNode= currentNode->next;        
        while(currentNode->ISBN > bookToInsert->ISBN){

            if (bookToInsert ->ISBN < currentNode->ISBN){
                // if isbn of current book more than current node, move to next current node
                //otherwise add here
                printf("Added");
                temp->next = currentNode->next;
                bookToInsert->next = currentNode->next;
                currentNode->next = temp->next;
            }
        }
    }
}

void displayList(Book* dummy){
    //start at dummy node-
    Book* currentNode = dummy;
    bool run = true;

    //print until next = null
    while(run==true){
        if (currentNode->next != NULL){
            printf("%s %s %ld \n", currentNode->title, currentNode->author, currentNode->ISBN); 
            currentNode = currentNode ->next;
        } else {
            run = false;
        }
    }    
}

该程序旨在创建作为链表节点的书籍结构。一本书在头文件Book.h中定义如下:

#ifndef BOOK_H
#define BOOK_H

#ifdef  __cplusplus
extern "C" {
#endif


typedef struct book_t {
    char title[50];
    char author[30];
    long ISBN;
    struct Book *next;
} Book;

Book* newBook(char* newTitle, char* newAuthor, long newISBN);

#ifdef  __cplusplus
}
#endif

#endif  /* BOOK_H */

我觉得我的 insertToList 函数已经接近工作了,但是由于看得太错误而导致我代码失明,而且我确信它确实存在一些基本的错误。目前没有输出 - 只是一个空终端,我相信循环没有正确退出。取消注释 printf 语句“added”和“head”会导致程序无限循环,将“added”输出到终端。

最佳答案

The structure should be:

typedef struct Book {
    char title[50];
    char author[30];
    long ISBN;
    struct Book *next;
} Book;

The code could be:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Book.h"

Book *newBook(char *newTitle, char *newAuthor, long newISBN);
void displayList(Book *dummy);
void insertToList(Book *bookToInsert, Book *dummy);
void freeList(Book *head);

int main(void)
{
    Book *dummy = newBook("dummy", "dummy", 00000);

    printf("%s %s %ld\n", dummy->title, dummy->author, dummy->ISBN);

    printf("Newish\n");
    Book *newishBook = newBook("Foo", "Chris", 1234);
    insertToList(newishBook, dummy);
    displayList(dummy);

    printf("Another\n");
    Book *another = newBook("Bar", "Jim", 23344);
    insertToList(another, dummy);
    displayList(dummy);

    printf("Yet\n");
    Book *yet = newBook("Derp", "Bob", 999);
    insertToList(yet, dummy);
    displayList(dummy);

    //searchISBN(999);
    freeList(dummy);

    return(EXIT_SUCCESS);
}

Book *newBook(char *newTitle, char *newAuthor, long newISBN)
{
    Book *new_book = malloc(sizeof(Book));

    strcpy(new_book->title, newTitle);
    strcpy(new_book->author, newAuthor);
    new_book->ISBN = newISBN;
    new_book->next = NULL;
    return new_book;
}

void insertToList(Book *bookToInsert, Book *dummy)
{
    Book *currentNode = dummy;

    while (currentNode->next != NULL && currentNode->next->ISBN < bookToInsert->ISBN)
        currentNode = currentNode->next;
    bookToInsert->next = currentNode->next;
    currentNode->next = bookToInsert;
}

void displayList(Book *dummy)
{
    Book *currentNode = dummy;

    while (currentNode != NULL)
    {
        printf("%s %s %ld\n", currentNode->title, currentNode->author, currentNode->ISBN);
        currentNode = currentNode->next;
    }
}

void freeList(Book *head)
{
    Book *bp = head;
    while (bp != 0)
    {
        Book *bn = bp->next;
        free(bp);
        bp = bn;
    }
}

This runs leak-free under valgrind thanks to the freeList() function being added and used.

Note how the list is printed after each entry is added.这有助于确保列表正确构建。还请注意,每条输出线如何用newline结束。在打印新线之前,您不一定会看到打印数据。特别是要调试,请确保您包括新线 - 但即使不调试,这通常是一个好主意。

关于c - C 中的链接列表 - 在排序位置添加结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146392/

相关文章:

delphi - Delphi 2009 的通用链表

c - 我无法在链表中使用链表

c - 为什么在 C 中遍历单链表时 NULL 指针检查不起作用?

c - 下三角矩阵和上三角矩阵给了我错误的答案

c - 如何与玩家和 AI 交替移动

c - 从 C 中的不兼容指针类型警告返回

C++ 迭代器 : no appropriate default constructor available

c - C中的链表,无法插入和显示节点

c - 什么是 printf(),语句或表达式?为什么它与三元运算符一起工作?

c - 这段代码在 C 中意味着什么, "int x = ~!printf; "?