# Thread: help with question from exam in c...

1. ## help with question from exam in c...

Given the following definition:

Code:
```typedef struct node node;struct node{
char* str;
node* next;
};```
And this function:

Code:
```void what(node* x){  node *temp, *y;

if (!x)
return;

y = x->next;
while (y && x->str[strlen(x->str)-1] != y->str[0]) {
temp = y;
x->next = temp->next;
free(temp);
y = x->next;
}

what(y);
}```
The List is:
Hello -> open -> all -> leave -> never -> radio -> table

1. What the new list after call to what(list)?
2. What is the function purpose is what?

Thanks!

2. Let's have a look at that code here:

Code:
```void what(node* x)
{
node *temp, *y;

if (!x) /* if x == NULL, return */
return;

y = x->next; /* y refers to the node next to x */
while (y && x->str[strlen(x->str)-1] != y->str[0]) /* While y != NULL and x's last char != y's first char */
{
temp = y;
x->next = temp->next; /* Move the list backwards */
free(temp);   /* Free the detached node */
y = x->next;
}

what(y); /* Will be recursively called until y == NULL */
}```
Isn't it obvious now?

3. This comment confuses me.
Code:
`x->next = temp->next; /* Move the list backwards */`
I would do this; does it makes sense to you?
Code:
`x->next = temp->next; /* Remove temp from the list */`
Tim S.

4. Originally Posted by stahta01
I would do this; does it makes sense to you?
Code:
`x->next = temp->next; /* Remove temp from the list */`
Yeah, that's what I meant, I don't know why I wrote it the way I did.

5. Originally Posted by GReaper
Let's have a look at that code here:
...
Isn't it obvious now?
There's nothing quite like the feel you get after doing someone's homework for them without any effort on their part, right?

Quzah.

6. Code:
`y = x->next; /* y refers to the node next to x */`
this is command is not entirely clear.

in first x equal "hello" right?
and y after this command equal x->next equal "open"?

thanks.

7. Originally Posted by quzah
There's nothing quite like the feel you get after doing someone's homework for them without any effort on their part, right?
o_O Well, I didn't post anything new, I just explained his code a bit!

8. Originally Posted by engti
in first x equal "hello" right?
and y after this command equal x->next equal "open"?
Change "equal" to "refers to", you'll make your like easier.

9. Originally Posted by GReaper
o_O Well, I didn't post anything new, I just explained his code a bit!
Except that's exactly what he wanted!

1. What the new list after call to what(list)?
2. What is the function purpose is what?
In other words, the overall question was "Figure out the logic of this function". You did that via your comments.

10. Originally Posted by rags_to_riches
In other words, the overall question was "Figure out the logic of this function". You did that via your comments.
Well, I didn't draw a flowchart!