Thread: How to reset a linked list i reversed

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    103

    How to reset a linked list i reversed

    I have a reverse method that messes with the original linked list inputted; however usually this is fixable by just reversing.

    my problem is after i reverse it, how do i recover the original linked list?

    Code:
    //compares p and q; if p is smaller, then return -1; if q is smaller, return 1
    //return 0 if its exactly the same
    int compare(struct integer* p, struct integer* q) {
       int count=1, count2=1;
       struct integer *ptest=p, *qtest=q;
       while(ptest->next!=NULL) {
          ptest=ptest->next;
          count++;            
       }
       printf("count p= %d\n",count);
       while(qtest->next!=NULL) {
          qtest=qtest->next;
          count2++;            
       }
       //PTEST AND QTEST ARE CURRENTLY POINTING TO LAST DIGIT OF P AND Q
       printf("count q= %d\n",count2);
       if(count<count2) {
          printf("p is less than q; returning -1\n");
          return -1;
       }
       else if(count>count2) {
          printf("q is less than p; returning 1\n");
          return 1;  
       }
       else {
          printf("p=%d and q are equal length; no return yet\n",ptest->digit);
          if(ptest->digit<qtest->digit) {
             printf("returning -1\n");
             return -1;               
          }
          else if(ptest->digit>qtest->digit) {
             printf("returning 1\n");
             return 1;  
          }
          else {
             
          }
          return 0;
       }
    }
    }
    
    //---------------PART OF ANOTHER FUNCTION; SEGMENT
    
          p=reverse(p);
          q=reverse(q);
          printf("\nRevp: ");
          print(p);
          printf("-Revq: ");
          print(q);
          printf("\n");
          while(p!=NULL && q!=NULL) {
             if(p->digit<q->digit) {
                printf("first else\n");
                p=reverse(p);
                q=reverse(q);
                free(ptest);
                free(qtest);
                return -1;           
             }
             else if(p->digit>q->digit) {
                printf("second else\n");
                p=reverse(p);
                q=reverse(q);
                free(qtest);
                free(ptest);
                return 1;
             }
          p=p->next;
          q=q->next;
          }
          return 0;
    Last edited by Soulzityr; 02-16-2010 at 04:48 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well if your reverse function works properly, reverse it again.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    ohh! maybe i should explain better;

    i do reverse it again, however, due to the p=p->next; and q=q->next at end of while to compare, i lose the ones at the front of the number, makes sense???

    the problem is i tried a struct integer* p=altp and substituted all the stuff to altp so i'd save the address of the first number, but it doesnt work =/ same output

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then your reverse function doesn't work. If it did, you would simply call it over again:
    Code:
    type *list;
    ...
    rev( &list ); /* or... */
    list = rev( list );
    ...
    /* later on... */
    ...
    rev( &list ); /* or... */
    list = rev( list );
    So, what's your reverse function look like, and how are you calling it?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    my reverse function is up top...and it works just fine...just messes up the pointers up so it cant reclaim the previous order; i usually do just recall it twice; but the problem is i cant because the pointers move during the while loop, and when i have holder pointers, it screws up
    Last edited by Soulzityr; 02-16-2010 at 03:35 PM.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    my problem is that after i run these two methods, the number

    6347 becomes 347 =[ i made a current pointer

    struct integer* current=p; then did all my stuff with p, and then made p=current;, but its cut off, dishambled, many different kind of errors, hep lz?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  2. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  3. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM