Thread: Sort doubly linked list.

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    2

    Sort doubly linked list.

    Hello everyone, I'm new on this forum and I have problem with my code. I'm learning C and want to use bubble sort to sort my doubly linked list. Here is my code:
    Code:
    struct node {                
         unsigned int key;
         struct node *left;
         struct node *right;
                };
    
    void sort(int count, struct node *t_node)
    {
        struct node *tmp,*nextnode, *node1, *node2;
    
    
        for(node1 = t_node;node1->right != NULL ;node1 = node1->right) {
            for(node2 = node1; node2->right != NULL; node2 = node2->right) {
        
                if(node2->key > node2->right->key)
                {            
                    
                    // B = node2
                    nextnode = node2->right; // C
                    tmp = node2->left; // A 
                    
                    if (tmp) 
                        tmp->right = nextnode; // A->right = C
                    
                    if (nextnode->right)
                        nextnode->right->left = node2; //D->left = B
                    node2->right = nextnode->right; //B->right = D
                    node2->left = nextnode->left; //B->left = C
                    nextnode->right = node2; //C->right = B
                    nextnode->left = tmp; //C->left = A
                }
            }
        }
        
    }
    When I have for example 4 nodes, with data:

    Node value: 255
    Node value: 24
    Node value: 7
    Node value: 36

    After using sort function I have:

    Node value: 255
    Node value: 7
    Node value: 24
    Node value: 36

    Where is the problem I don't have idea. Please help me, thanks.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    are you trying to use bubble sort with list?

    I would use insertion sort instead


    For your problem - try to use debugger - or add print_list calls after each loop iteration and investigate what is going on step by step
    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

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Looking at your output - looks like your sort has worked - you just forgot to reassign the head to the new minimum... (if it is cyclic list) - or your swap algorithm does not work for head node
    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

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    2
    How can I reasign the head. I need to do it in my loop or outside of it?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can change your sort function to return the new head instead of void
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with my doubly linked list
    By evildotaing in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2011, 11:47 AM
  2. merge sort over (doubly) linked lists
    By zxcv in forum C Programming
    Replies: 5
    Last Post: 11-29-2010, 12:10 PM
  3. Help with doubly linked list
    By Furbiesandbeans in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2008, 11:41 AM
  4. doubly linked list
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-26-2007, 03:41 AM
  5. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM

Tags for this Thread