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
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.
Re: having problems sorting a linklist
Quote:
Originally Posted by
terrorofdeath
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
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
Re: having problems sorting a linklist
do you guys know any algorithm visualization program for c++
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.