hi as you may notice i am new here so hi nice to meet you:)..

uhhhmmm please help me on a project i'm working on about insertion using linked list my problem is on line 19-20 I can't seem to make it work,, when you type 9 7 6 4 2 it will display 9 2 4 6 7 so im pretty sure i'm very close can you point out what's wrong with my code tnx a lot :D

this is what i've done so far

void insertion_sort (int n) 
    struct node *p;
node * temp1 = NULL;
node * temp2 = NULL;
node * temp3 = NULL;

for (int i=1; i < n; i++)
    p = start;
    temp1 = p ;
    for (temp2 = p->link; temp2->link != NULL; temp2 = temp2->link) 

        if (temp2->data > temp2->link->data) 

            temp3 = temp2->link;
            temp1->link = temp2->link;
            temp2->link = temp3->link;
            temp3->link = temp2;

    temp1 = temp2;
        if( !temp2->link )

Bold Text Here

5 Years
Discussion Span
Last Post by Griff0527

Write it out in pseudo-code first, and then code it. IE:

n = new_value
p = start
lastp = null
while (p not null && n gt p->value)
    lastp = p;
    p = p->next
if p is null // add n to end
    if lastp is null
        // Nothing in list - create start node
        start = new node
        start->value = n
        // Append to end of list, represented by lastp
        lastp->next = new node
        lastp->next->value = n
    // Found insertion point
    // Now we need to know if n is a new start value
    // or to be inserted between p and lastp

This should not be difficult to finish and turn into proper C/C++/Java code.


I'm not positive, but it appears that you are starting the variable i at position 1, but should start at position 0? This would explain why the sort works correctly for all but the first position.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.