Thread: help with sorting a list :(

  1. #46
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    you mean something like this?

    Code:
    void swap(charNode **a, charNode **b, charNode *start) {
      charNode *temp, *preTemp;
      charNode *preA = pred(start->next, start);
      charNode *preB = pred(start, start);
    
      preTemp = preA;
      
      temp = *a;
      *a = *b;
      *preB = *preA;
      *b = temp;
      preB = preTemp;
      
    }

    *CRASH*

    this is killing me :|

  2. #47
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    No, like this :

    Code:
    charNode* temp2 = (*preA).next;
    (*preA).next = (*preB).next;
    (*preB).next = temp2;
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #48
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Code:
    void swap(charNode **a, charNode **b, charNode *start) {
      charNode *temp, *preTemp;
      charNode *preA = pred(start->next, start);
      charNode *preB = pred(start, start);
      charNode* temp2 = (*preA).next;
      
        temp = *a;
       *a = *b;
       *b = temp;
    	
    	(*preA).next = (*preB).next;
    	(*preB).next = temp2;
    	 
      
    }
    *CRASH*

  4. #49
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Oh for chrissakes.

    Code:
    void swap(charNode *a, charNode *b)
    {
            char temp;
            size_t i;
    
            for (i = 0; i < 12; i++)
            {
                    temp = a->petName[i];
                    a->petName[i] = b->petName[i];
                    b->petName[i] = temp;
            }
    }
    Last edited by zx-1; 10-15-2006 at 11:55 PM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  5. #50
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    zx-1, i was just about to post the progress i made. The only problem was that the solution could only swap start and next node. Your solution however if i input any node it swaps it..hmm


    i.e.:

    Code:
    void *swap(charNode **start, charNode *a, charNode *b)
    {
    
          	 charNode *d = a->next->next->next;
    
          	 a->next = b->next;       
          	 b->next->next = b;       
          	 b->next= d;      
    }

  6. #51
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    actually your solution doesnt swap the nodes at all but just the contents of the struct which is cheating? you also don't need that for loop. it works fine without it.
    Last edited by Axel; 10-16-2006 at 12:01 AM.

  7. #52
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    so i'm guessing its not possible to have a function like the one you have there that swaps the nodes and reconstructs the list? is it too complex? because i can swap the nodes if i know where they are but as soon as i try it on the middle node and the next for example it stuffs up the list.

    i.e.:

    works fine for swapping start and the next node.

    Code:
    	charNode *d = a->next->next;
          	charNode *head = start;
    		a = b;       
          	b->next = head;
          	b->next->next=d;
    this works fine for swapping the node after next and the one after next,next.

    Code:
    charNode *d = a->next->next->next;
    
          	 a->next = b->next;       
          	 b->next->next = b;       
          	 b->next= d;
    but i cant seem to make a unified solution that does swapping regardless of the possition ?!?

  8. #53
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Also, you have to swap the next pointer of each of the nodes you're swapping. I don't know why I didn't think of that. But that's important too. Also, you could very well just use zx-1's solution. It'll get the job done.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #54
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    yea agreed... it's better to actually swap the physical string than to modify the pointer

  10. #55
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I've always preferred to swap the the pointers around, as it just seems logical for me. But moving data accross nodes would work the same.

    Also, it's easy right now to just copy over an array, but when your structs start containing a little more data, you'll be spending a lot of time copying everything over...
    Last edited by Happy_Reaper; 10-16-2006 at 09:02 AM.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM