I've been wrestling with this for some hours and I'm wondering if anyone can see what's wrong with it.
Here is my full compileable, formatted code code: http://dustinnerayis.boldlygoingnowh...signment%204.c
Thanks a lot in advance!
Here are the necessary parts:
Code:
int main(void) {
srand ( time(NULL) );
BOOK books; // Both of these should be stored as pointers and malloc should be used to make things easier for myself but when I switch it and make the necessary changes, the program still crashes
MEMBER club;
club.next = NULL;
books.ISBN[0] = 'a';
books.next = &books;
..........
Code:
else if (userInput == 2) { // let's sort the books by title!
//books = *bookSortByTitle(&books);
BOOK* newStart = bookSortByTitle(&books);
BOOK* currentBook = newStart->next;
//while (currentBook->next != newStart) {
// currentBook = currentBook->next;
// }
currentBook->next = &books;
books = *newStart;
printf("\nI sorted your books alphabetically by title\n");
}
Code:
BOOK * bubbleSortBookTitle(BOOK * head, int count)
{
BOOK *parent, *newhead, *temp;
int again = 1;
newhead = head;
while(again)
{
head = newhead; /* set head to the first element in the list */
parent = NULL; /* current head is first element, it has no parent */
again = 0; /* should we sort again? only if a swap is made */
int i;
for(i=0; i<count-1; i++)
{
if( compareBooks(head, head->next) == -1 )
{
/* if head->next should come before head then swap them */
temp = head->next;
head->next = head->next->next;
temp->next = head;
head = temp;
if(parent != NULL) /* if this is not the first element in the list */
parent->next = head;
else /* if there is no parent to this head, that means this is the
beginning of the list so we'll set the newhead to this */
newhead = head;
again = 1; /* we made a swap so we'll have to go through the while loop again */
}
parent = head;
head = head->next;
}
}
return newhead;
}
Code:
int compareBooks(BOOK * first, BOOK * second)
{
return compareStrings(first->title, second->title);
}
Code:
BOOK* bookSortByTitle(BOOK* firstBook) {
int elements = 1;
BOOK* testBook = firstBook->next;
while (testBook != firstBook) {
testBook = testBook->next;
elements++; }
return bubbleSortBookTitle(firstBook, elements);
}