i know how some parts of sorting linklist works but not all of it what does the rest of the code do
what i dont understand is this part of sorting aCode:#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; }
linklist
what does this part of the code doCode: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; }




Reply With Quote
