Still struggling with sorting a singly linked list

This is a discussion on Still struggling with sorting a singly linked list within the C Programming forums, part of the General Programming Boards category; typedef struct message { int messageId; char * messageText; struct message * next; } message; void BUBBLEsort(message* a, int n) ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    6

    Still struggling with sorting a singly linked list

    typedef struct message {
    int messageId;
    char * messageText;
    struct message * next;
    } message;

    void BUBBLEsort(message* a, int n)
    {
    int bound = n-1, done = 0;
    int swapped, i;
    do
    {
    swapped = -1;
    for (i = 0; i < bound; i++)
    {
    printf("i is %d\n", i);
    if (compare(a, a->next))
    {
    swap(a, a->next);
    swapped = i;
    }
    a = a->next;
    }
    if (swapped < 0)
    done = 1;
    else
    bound = swapped;
    } while (!done);
    }


    int compare(message *a, message *b) /* if a > b it returns 1 */
    {
    if(a->messageId > b->messageId)
    return 1;
    }

    void swap(message *a, message *b)
    {
    int temp = a->messageId;
    a->messageId = b->messageId;
    b->messageId = temp;
    }
    I posted earlier questions about sorting linked list and so far I couldn't find a ready to use implementation so I am struggling with hacking similar code to work with my linked list.
    I get a BUS error with this code.
    Help please.
    Thank you.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Please indent next time.

    Code:
    typedef struct message {
    	int messageId;
    	char * messageText;
    	struct message * next;
    } message;
    
    void BUBBLEsort(message* a, int n)
    {
    	int bound = n-1, done = 0;
    	int swapped, i;
    	do
    	{
    		swapped = -1;
    		for (i = 0; i < bound; i++)
    		{
    			printf("i is &#37;d\n", i);
    			if (compare(a, a->next))
    			{
    				swap(a, a->next);
    				swapped = i;
    			}
    			a = a->next;
    		}
    		if (swapped < 0)
    			done = 1;
    		else
    			bound = swapped;
    	} while (!done);
    }
    
    
    int compare(message *a, message *b) /* if a > b it returns 1 */
    {
    	if(a->messageId > b->messageId)
    		return 1;
    }
    
    void swap(message *a, message *b)
    {
    	int temp = a->messageId;
    	a->messageId = b->messageId;
    	b->messageId = temp;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    int compare(message *a, message *b) /* if a > b it returns 1 */
    {
    	if(a->messageId > b->messageId)
    		return 1;
    }
    And what you will return when if condition is false?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't have any way to get back to the beginning of your list; the first go through, you walk the entire thing; then when you start the next loop you try to compare the last guy with NULL->messageId and all hell breaks loose. Make a copy of your pointer a at the start, and at the top of the loop reset a to be your copied first node.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 05:37 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21