Hi, Im having a problem with my (Bubble?)sorting of linked list, Example i have FName on each node in this order, E-D-C-B-A, it sorts well,Code:void SortRecipe(Recipe **pFirst){ int swap; Recipe *pHead,*pTail,*pRun,*pLast; pRun=(*pFirst)->pNext; pLast=*pFirst; do{ pTail=*pFirst; pHead=*pFirst; swap=0; do{ if(pTail->pNext!=NULL) pHead=pTail->pNext; printf("Moves pHead %s %s\n",pTail->FName,pHead->FName); if(strcmp(pTail->FName,pHead->FName)>0 && pHead!=NULL){ pTail->pNext=pHead->pNext; pHead->pPrev=pTail->pPrev; if(pTail!=*pFirst) pHead->pPrev->pNext=pHead; else *pFirst=pHead; pHead->pNext=pTail; pTail->pPrev=pHead; swap=1;printf("Swaps %s %s\n", pTail->FName,pHead->FName); printf("%s\n",pHead->pNext->FName); } else{ pTail=pTail->pNext; printf("No Swapping %s\n",pTail->FName); printf("%s\n",pHead->pNext->FName); } pHead=pTail; printf("Finished 1 round\n"); }while(pTail->pNext!=NULL); printf("Finished Outer Round"); }while(swap==1); (*pFirst)->pPrev=NULL; while(pRun->pNext!=NULL) { pRun->pPrev=pLast; pLast=pLast->pNext; pRun=pRun->pNext; } system("pause"); }
but X-C-V-Z-A would destroy the program. It first sort X-C,
would be C-X-V-Z-A then sort X-V, would be C-V-X-Z-A. Sorting X-Z(wont change anything) then sort Z-A, then the whole list would be..C-V-X-A-Z
but after it returns to C to continue the bubble sort, the link would somewhat look like C-V-A-Z, losing the node of X. Please help. Thank you.