0
I’m following the notes of Algorithm Projects by Professor Paulo Feofiloff, and I’m having a hard time solving a question from the Chained Lists, transcript to follow:
Exercises 5
- Write a function that inverts the order of cells in a chained list (the first becomes the last, the second becomes the penultimate etc.). Do this without using auxiliary space, just changing pointers. Give two solutions: an iterative and a recursive.
I was able to come up with an iterative solution, but I couldn’t even draw a recursive solution.
So I ask you to direct me to the recursive solution of this problem. Thank you!
Follow iterative solution code (please indicate improvements).
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} NODE;
/*
Esta função recebe uma lista encadeada de inteiros e inverte a posição das
células desta lista usando apenas ponteiros. Ex.: o primeiro nó troca de lugar
com o último, o segundo nó troca de lugar com o antepenúltimo, etc.
*/
// versão iterativa
void swap_nodeI (NODE **head) {
// if list is empty or there is only one node, nothing to do
if (*head == NULL) return;
// declare variables for first half of the list
NODE *prevX = NULL, *currX = *head;
int countX = 1;
// declare variables for second half of the list
NODE *prevY = NULL, *currY = *head;
int countY = 1;
// count nodes (countY)
for (; currY->next != NULL; currY = currY->next, ++countY);
// swap nodes
NODE *temp;
int i, j;
while (countX < countY) {
// setup pointers
for (currY = *head, i = 1; i != countY; ++i) {
prevY = currY;
currY = currY->next;
}
for (currX = *head, j = 1; j != countX; ++j) {
prevX = currX;
currX = currX->next;
}
// swap X and Y
if (prevX != NULL) prevX->next = currY;
else *head = currY;
if (prevY != NULL) prevY->next = currX;
else *head = currX;
temp = currY->next;
currY->next = currX->next;
currX->next = temp;
// update counters
++countX;
--countY;
}
}
I’ve already answered a question like this...
– Jefferson Quesado
https://answall.com/a/217757/64969
– Jefferson Quesado
Thanks, Jefferson! I was racking my brain to find a recursive solution that didn’t involve reporting the size of the chain list. Do you know if it is possible like this? Thank you anyway. I will try to implement with your strategy.
– fullone
Yes, the idea is to make the inversion between the previous and the following,
anterior -> atual -> seguinte
foranterior <- atual <- seguinte
– Jefferson Quesado
My God.. just now the plug fell. I was set on exchanging equidistant pairs! When it was enough to invert the entire list..
– fullone
Please do not use the question to post the answer. Use the field below to answer with your solution.
– user28595
Answer my question?
– fullone
Yes, in the field below the button "publish your answer"
– user28595