Jump to content
xisto Community

nroot1

Members
  • Content Count

    1
  • Joined

  • Last visited

Everything posted by nroot1

  1. Hey everybody,I've been stuck on this for the past 4 hours now. I need to write a function that will swap the first and last nodes of a linked list, leaving the middle the sameconstraints -cannot move data, only the pointerssample output:linked list a is: 10 20 30 40 50 60 70after swapping: 70 20 30 40 50 60 10so far i'm only able to get this: 70 20 30 40 50 60 void reverse( node * & s) { if(s == NULL || s->p == NULL) //0 or 1 node { //do nothing } else if(s->p->p == NULL) //2 nodes { node *temp = s; s = temp->p; temp->p = NULL; s->p = temp; } else { node *result = NULL; node *cur = s; node *walker; //reverse order while (cur != NULL) { walker = cur->p; cur->p = result; result = cur; cur = walker; } s = result; result = NULL; cur = s->p; s->p = NULL; //reverse all but first & last node while (cur->p != NULL) { walker = cur->p; cur->p = result; result = cur; cur = walker; } s->p = result; }} there's probably a simpler way to do this...but after working on it for so long my brain is fried lolall i need to do now is figure out a way to get the original first node @ the end of the listthanks!
×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.