#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”会导致程序无限循环,输出“添加”到终端。
答案 0 :(得分:1)
insertToList函数不处理所有情况。
首先检查列表是否为空(正确) while循环开始缺少逻辑。你应该:
1) check if currentNode is larger (same as your if statement)
a) if it is smaller, insert the book,
b) if it is larger, you need another check:
i) if currentNode has next !== NULL, move down and repeat loop
ii) if next == NULL, add book at the end and return;
答案 1 :(得分:1)
结构应该是:
typedef struct Book {
char title[50];
char author[30];
long ISBN;
struct Book *next;
} Book;
代码可以是:
#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;
}
}
由于添加并使用了freeList()
功能,这在valgrind
下无泄漏。
注意添加每个条目后如何打印列表。这有助于确保正确构建列表。另请注意每行输出如何以换行结束。在打印换行符之前,您不一定会看到打印数据。特别是对于调试,请确保包含换行符 - 但即使不进行调试也通常是个好主意。