> Pointer c programming?

Pointer c programming?

Posted at: 2014-12-18 
Since x is a pointer a list entry, x->data and x->next are the fields in the struct that x points to. You can also write those as (*x).data and (*x).next, which mean exactly the same thing but are a bit harder to type.

Since x points to the first entry, x->data is 34, and x->next is a pointer to the 2nd entry in the list. That makes x->next->data and x->next->next the fields in the struct that x->next points to, so x->next->data is 74.

I'd write the body of the Negate function as a for statement in C. The generic "browse a linked list starting at x" loop for C99 looks like:

for (struct il *ptr=x; ptr!=NULL; ptr=ptr->next )

{

.... do something with *ptr as the current list entry

}

For older compilers, you'll have to declare ptr at the top of the function body, and just use "for (ptr=x; ptr!=NULL; ptr=ptr->next)" as the for loop header.

That will visit each entry in the list once, in order, with ptr pointing to the current entry being visited. The Negate() function will just use ptr->data = -ptr->data; as the body of the loop, negating the data of each visited list entry.

34

Assuming NULL terminates the linked list:

void Negatelist(struct il *list)

{

struct il *p;

p = list;

while(p->next != NULL)

{

p = p->next;

p->data = -p->data;

}

}

Ii is a linked list with following definition:

struct il {

int data;

struct il *next;

};

If x is a pointer to the first node in a linked list with three nodes containing data with the values 34, 74, -29.

What value has then x->next->data for value ?

Is it 34, 74 -29 ?????

And write a function Negatelist(struct il *list), that negate the value on data in all nodes. Ex:if x points on the list {-24,1,42} so shall NegateList(x) be changed to {24,-1,-42}

How can i do that ?