Compare both the programs below with the marker =>
A).
struct node {int value; struct node *next;);
void rearrange (struct node *list) {
struct node *p, *q;
int temp;
if (!list || !list -> next) return;
p = list; q = list -> next;
while (q) {
temp = p -> value;
p -> value = q -> value;
q -> value = temp;
p = q -> next;
q = p ? p -> next : 0;
}
}
Clearly, swapping is being done so output for this is 2, 1 ,4 ,3, 6 ,5, 7
B).
struct node {
int value;
struct node *next;
};
void rearrange (struct node *list) {
struct node *p, *q;
int temp;
if (!list || !list -> next) return;
p = list; q = list -> next;
while (q) {
temp = p -> value; p -> value = q -> value;
q -> value = temp;
p = q;
q = p ? p -> next : 0;
}
}
Clearly, again swapping is being done, but in a different way. Output is 2,3,4,5,6,7,1 .