#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;
}
}
}
This program is intended to create book structs which are the nodes of a linked list. A book is defined as follows in the header file 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 */
I feel like my insertToList function is close to working, but I've got code-blindness from looking at it too wrong and I'm sure there's something really basic wrong with it. Currently there is no output- just an empty terminal, I believe the loop is not exiting properly. Uncommenting the printf statments "added" and "head" causes the program to loop indefinitely, outputting "added" to the terminal.
newBook()should ensure all fields are initialized. You should not need to dodummy->next = NULL;inmain()— that should be done bynewBook().typedef struct book_t { …; struct Book *next; } Book;. You don't show thestruct Bookto which thenextpointer points — it is not thestruct book_t. So you should be getting warnings about pointer type mismatches.while(currentNode->ISBN > bookToInsert->ISBN)will never exit because the variables used in the condition never change inside the loop. Definitely, you need to think again yourinsertToListfunction.tempininsertToList()is puzzling; it looks likely to leak memory. You're about to insertbookToInsert, so it isn't clear why you needtempexcept to confuse everyone.