Thread: Trouble using Insertion Sort on Double Linked List

  1. #16
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    Thank you, this makes a lot of sense.
    I'm getting two problems on line 96 that have to do with memory allocation:
    'use of uninitialized value of size 8' and 'invalid write of size 8'

    I have been seeing these pop up but I'm still not entirely clear on what they mean, but it looks like this is the cause of the segmentation fault I'm getting.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Line 96? Of what version? Surely you've changed your code since the last posting. Please the updated version.

    EDIT: And yes, they seem to indicate memory errors. Do you know how to use a debugger? It would really help here. The exact details depend on which debugger you use, which depends (somewhat) on which compiler/compiler options you use.

  3. #18
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    I have been using valgrind to find these errors and problems. The submission deadline for this assignment actually just passed and I had to submit an incomplete non-working version for now. I am going to ask my professor for help tomorrow; he said he may extend the deadline. I will post back on here when I have more information and have hopefully resolved some of the problems. Thanks for all your help.

  4. #19
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    With the help of GDB, my professor, and a friend, I managed to finally get a working Insertion Sort function.

    Code:
    void Insertion_sort(List * list)
    {
        Node * index;
        Node * node;
        Node * temp;
        Node * temp2;
    
        for(index = list->first; index != NULL && index->next != NULL; index = index->next)
        {
            if(index->ID > index->next->ID && &index->next->ID != NULL)
            {
                node = remove_node(list, index->next);
                temp = index;
    
                while(temp->ID > node->ID)
                {
                    if(temp != list->first)
                    {
                        temp = temp->prev;
                    } else {
                        break;
                    }
                    
                }
                if(temp->ID < node->ID)
                {
                    temp2 = temp->next;
                    node->prev = temp;
                    node->next = temp2;
                    temp->next = node;
                    temp2->prev = node;    
                } else {
                    node->next = temp;
                    list->first = node;
                    temp->prev = node;
                }
                index = index->prev;
            }
            
            if(index == list->last)
            break;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion Sort with Linked Lists
    By Linell Bonnette in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2011, 11:46 PM
  2. Insertion Sort and Linked List
    By dlwlsdn in forum C Programming
    Replies: 2
    Last Post: 01-25-2010, 11:25 PM
  3. Help with Insertion sort on a single linked list
    By goron350 in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2005, 08:38 PM
  4. Linked List Insertion Sort
    By The Brain in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2005, 07:20 PM
  5. Sort strings in double linked list. (Optimize code)
    By netstar in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2005, 01:40 AM

Tags for this Thread