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;
if(q)
{
temp=p->value;
p->value=q->value;
q->value=temp;
q=p?p->next:0;
}
}
}

list is containing the integers 1,2,3,4,5,6,7
then the output is?
also what's the meaning of p=list(as per me its the HEADER, then p->value is 0 or something else? confirm)

Recommended Answers

All 4 Replies

Since you are in this forums, you are obviously studying C. And studying C requires that you should have a compiler to crunch your code. Why don't you run the code and see the result/output for yourself instead of asking?

As for the last question, saying

p = list;

means that you are making p "point" to the same location that list "points" to.

Consequently, you will observe that the values of the members of p and list are the same:

p->value == list->value;
p->next == list->next;

Since the function does not have a loop variable p can be removed and line 15 deleted. Line 15 is wrong anyway because it used = assignment operator instead of == boolean operator. Also, without a loop line 15 does nothing even then the == operator is corrected.

>> what's the meaning of p=list(
To initialize the variable p.

The code manipulates the structure through the passed pointer doesn't it? i think you are mistaken Oh Ancient One! Ternary operation in line 15 set node's pointer to equal p's next pointer if p is not null pointer or zero if p is zero (NULL).

The code manipulates the structure through the passed pointer doesn't it? i think you are mistaken Oh Ancient One! Ternary operation in line 15 set node's pointer to equal p's next pointer if p is not null pointer or zero if p is zero (NULL).

Yes, but what good is it? The function sets pointer p then just exists without doing anything with it. That makes it a do-nothing operation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.