having problems sorting a linklist
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: having problems sorting a linklist

  1. #1
    Join Date
    Apr 2013
    Posts
    6

    having problems sorting a linklist

    i know how some parts of sorting linklist works but not all of it what does the rest of the code do

    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    struct linklist 
    {
      int data;
      struct linklist * pnext;
      
    };
    
    struct linklist * add(struct linklist * object,int value)
    {
       struct linklist * temp;
       if(object == NULL)
       {
         object = (struct linklist *)malloc(sizeof(struct linklist));
         object->data = value;
         object->pnext = NULL;
       }
       else
       {
           temp = object;
           while(temp->pnext != NULL)
           temp = temp->pnext;
           
           temp->pnext = (struct linklist *)malloc(sizeof(struct linklist));
           temp = temp->pnext;
           temp->data = value;
           temp->pnext = NULL;
       }
       
       return (object);
    }
    
    void print(struct linklist * obj)
    {
      do
      {
        cout << " value " << obj->data <<endl;
        obj = obj->pnext;
      }while(obj != NULL);
    }
    
    struct linklist *generator(struct linklist * object,int size)
    {
      for(int i = 0; i < size; i++)
      {
         int rnd = rand() % 100 + 0; 
         object = add(object,rnd);
      }
      return (object);
    }
    
    struct linklist *sortlist(struct linklist *p)
    {
       struct linklist *temp1,*temp2,*min,*prev,*q;
    
       q = NULL;
       while(p != NULL)
       {
             prev = NULL;
          min = temp1 = p;
          temp2 = p->pnext;
          while ( temp2 != NULL )
          {
                  if(min->data > temp2-> data)
             {
                         min = temp2;
                         prev = temp1;
             }
             temp1 = temp2;
             temp2 = temp2-> pnext;
          }
          if(prev == NULL)
          p = min->pnext;
          
          else
          prev->pnext = min->pnext;
          
          min->pnext = NULL;
          if( q == NULL)
          q = min;
             else
             {
                temp1 = q;
                while( temp1->pnext != NULL)
                temp1 = temp1->pnext;
                
                temp1->pnext = min;
          }
       }
       return (q);
    }
    
    
    int main()
    {
        struct linklist * object = NULL;
        object = generator(object,10);
        
        void (*print1)(struct linklist *) = &print;
        (*print1)(object);
        
        struct linklist * (*sort)(struct linklist *) = &sortlist;
        object = (*sort)(object);
        
        (*print1)(object);
        
       
        system("pause");
        return 0;
    }
    what i dont understand is this part of sorting a
    linklist

    Code:
    if(prev == NULL)
          p = min->pnext;
          
          else
          prev->pnext = min->pnext;
          
          min->pnext = NULL;
          if( q == NULL)
          q = min;
             else
             {
                temp1 = q;
                while( temp1->pnext != NULL)
                temp1 = temp1->pnext;
                
                temp1->pnext = min;
          }
    what does this part of the code do

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,452

    Re: having problems sorting a linklist

    The best way to understand what is going on is to do what we would have to do and that is to draw some diagrams representing the linked list with lines and arrows representing pointers etc and work through the code on paper with an example and change the diagram as stated by the code as its worked through. Then you'll see what is happening to the various pointers etc and then you'l know how the code works.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,429

    Re: having problems sorting a linklist

    Quote Originally Posted by terrorofdeath View Post
    i know how some parts of sorting linklist works but not all of it what does the rest of the code do
    Did you write the code? If you did, then you're supposed to understand what you wrote, else you wouldn't have written the code.

    Use your debugger to follow the code if you don't understand what is happening.

    Also, I'm assuming that some person not versed in C++ wrote the code, for example:
    Code:
    struct linklist 
    {
      int data;
      struct linklist * pnext;
    };
    
    struct linklist * add(struct linklist * object,int value)
    {
       struct linklist * temp;
       if(object == NULL)
       {
    object = (struct linklist *)malloc(sizeof(struct linklist));
    1) There is no need for redundantly stating "struct" in a C++ program.
    2) Why in the last line in red are you using malloc() instead of operator new?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 2013
    Posts
    6

    Re: having problems sorting a linklist

    never mind i understand what is going with the code i used visual c++ and bloodshed to debug my program

  5. #5
    Join Date
    Apr 2013
    Posts
    6

    Re: having problems sorting a linklist

    do you guys know any algorithm visualization program for c++

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: having problems sorting a linklist

    Personally, I prefer to write pseudocode, but if you want to be visual, use pencil and paper to draw a flowchart.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center