Thread: Sorting Problem,

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    12

    Sorting Problem,

    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");
    }
    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,
    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.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    Well, my problem is..tracing it for around 1hr.. V is suppose to be connected to X, but after it loops back the outer loop, V will suddenly be pointing to A O_O

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Problem
    By GravyBoatCaptn in forum C++ Programming
    Replies: 1
    Last Post: 07-23-2010, 08:39 AM
  2. Problem with sorting
    By preeengles in forum C Programming
    Replies: 35
    Last Post: 04-22-2009, 07:45 PM
  3. Sorting problem?
    By audinue in forum C Programming
    Replies: 5
    Last Post: 01-06-2009, 02:16 PM
  4. Sorting problem
    By stimpyzu in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2002, 01:07 AM
  5. Can anyone help with sorting problem???
    By DanTheMan in forum C Programming
    Replies: 1
    Last Post: 04-04-2002, 08:58 AM