Thread: Can anybody see what's wrong with this code? it's supposed to sort a linked list alph

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Can anybody see what's wrong with this code? it's supposed to sort a linked list alph

    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);
    }

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Code:
    int compareStrings(char * first, char * second)
    {
    /* reached end of strings, they are equal */
    if(first[0] == '\0' && second[0] == '\0') return 0;
    
    /* if first characters are equal(non case sensitive) do a recursive call starting at the second character of each string */
    if ( toupper(first[0]) == toupper(second[0]) ) return compareStrings(first + 1, second + 1);
    
    /* return 1 if first comes before second, return -1 if second comes before first */
    if ( toupper(first[0]) < toupper(second[0]) ) return 1;
    else return -1;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To compare strings don't you want to use strcmp()? That would be the normal way to do it. You can always "roll your own" for this comparison, but I don't like the string comparison you now have in place.

    Just remember that the return from strcmp() will be based on 0 meaning strings are equal and the <> symbol "opening up" to the larger of the two strings - whatever the value returned may be. >0 means string1 is greater, and < 0 means string2 is greater:

    result = strcmp(string1, string2);
    Last edited by Adak; 04-01-2010 at 04:29 AM.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    strcmp treats capitals and lowercase differently thats why I have a different one

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Funzo View Post
    strcmp treats capitals and lowercase differently thats why I have a different one
    there is strcasecmp (for linux) or _stricmp fo VC++
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't try and use bubble sort on a linked-list. It's actually quite difficult to get right. You don't need to pass in the count either.
    Use Insertion Sort instead. It's the simplest linked-list sorting algorithm there is!
    Code:
    While old list is not empty
        pop item off old list
        insert item into new list
    return the new list
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circularly linked list
    By BlackOps in forum C Programming
    Replies: 0
    Last Post: 07-18-2009, 08:12 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM