sorting a link list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ericstein81
    New Member
    • Jul 2007
    • 14

    sorting a link list

    I have written a program and am having trouble with the bubblesort... i get very confused on it and am not too familiar with link lists in the first place. The link list runs succesfully when i dont have the bubble sort in the program and it does what its supposed to do. However, i need to sort both list. if i just have some clarification on what to do with my bubble sort on the first list i can figure it out on the second. I understand there is a nested for loop for the bubblesort as you can see but i dont have it set up properly. any help would be appreciated and thank you in advance.
    [code=cpp]
    void main()
    {
    cout << "Insert 12 numbers \n" ;
    node_type *first,*p,*q,*n ewnode,*temp;
    int i,a,b,j;
    float sum,average;
    sum = 0;
    first = new node_type;
    p = first;
    getdata(a);
    (*first).flist = a;
    (*first).next = null;
    for (i =0; i<=10;++i)
    {
    getdata(a);
    newnode = new node_type;
    (*newnode).flis t = a;
    (*newnode).next = null;
    //**********Links previous node to newnode******//
    (*p).next = newnode;
    p = newnode;
    }
    //**********Trave rses list and prints out nodes in chronological order ******//
    q = first;
    cout << "Unsorted list \n";
    while (q != null)
    {
    cout << (*q).flist << "\n";
    sum = sum + (*q).flist;
    q = (*q).next;
    }
    average = sum/12;
    cout << "\nThe average of the numbers is " << average << "\n";

    //*************** **Bubblesort*** *************** ***//

    for ((*q).flist = first; (*q).next != null; (*q).flist = (*q).next)
    {
    for ((*p).flist = (*q).next; (*p).next != null; (*p).flist = (*p).next)
    {
    if (q > p)
    {
    temp = q;
    q = p;
    p = temp;
    }
    }
    }


    q = first;
    cout << "Sorted list \n";
    while (q != null)
    {
    cout << (*q).flist << "\n";
    q = (*q).next;
    }

    //**********start of second list(b)******** *****//
    cout << "Insert 6 numbers \n" ;
    first = new node_type;
    p = first;
    getdata2(b);
    (*first).seclis t = b;
    (*first).next = null;
    for (i =0; i<=4;++i)
    {
    getdata2(b);
    newnode = new node_type;
    (*newnode).secl ist = b;
    (*newnode).next = null;
    //**********Links previous node to newnode******//
    (*p).next = newnode;
    p = newnode;
    }
    //**********Trave rses list and prints out nodes in chronological order ******//
    q = first;
    cout << "Unsorted list \n";
    while (q != null)
    {
    cout << (*q).seclist << "\n";
    q = (*q).next;
    }
    }[/code]
    Last edited by sicarie; Aug 28 '07, 12:48 PM. Reason: Code tags.
  • Dreea
    New Member
    • Aug 2007
    • 37

    #2
    1.in your bubble sort your loop initialization is of an int (int flist) while you loop trough pointers so the first modification is to loop with pointers
    [code=c"]
    for (q = first; (*q).next != null; q = (*q).next)
    {
    for (p = (*q).next; (*p).next != null; p = (*p).next)

    [/code]

    2. when you test the ordinal relationship between your list elements you should do that : [code="c"](*q).flist > (*p).flist [/code]

    3. when interchanging values you can simply interchange the flist component of your list
    [code="c"]
    temp = (*q).flist;
    (*q).flist = (*p).flist;
    (*p).flist = temp;
    [/code]

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      ericstein81-

      Please have a look at your PM (Private Messages) in the top right corner of the page.

      Thanks!

      Comment

      • ericstein81
        New Member
        • Jul 2007
        • 14

        #4
        Thank you Dreea. It was having a problem with sorting the last link on the list at first, but i got it to work.

        Comment

        Working...