Jump to content

Help please with question in C.

- - - - -

  • Please log in to reply
5 replies to this topic

#1
engti

engti

    Newbie

  • Members
  • Pip
  • 4 posts
Given the following definition:

typedef struct node node;struct node{ 

 char* str; 

 node* next; 

}; 

And this function:

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
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Have you stepped through this function on paper using drawings representing an example list? What have you found out about the behavior of this function after doing so?

Consider the following example list: (I'm using NODE[...] to denote a node struct, and arrows to show where pointers lead.)

example--> NODE[str="Hello",next]-->NODE[str="open",next]-->NODE[str="leave",next]-->NODE[str="never",next]-->NODE[str="radio",next]-->NODE[str="table",next]-->null


Draw all the parts of this list out on paper and step through the code. First, you pass the list into the function, where it gets represented by the pointer x. Draw x pointing to the front of the list. Draw pointers for the local variables 'temp' and 'y'.


example--> NODE[str="Hello",next]-->NODE[str="open",next]-->NODE[str="leave",next]-->NODE[str="never",next]-->NODE[str="radio",next]-->NODE[str="table",next]-->null

           ^

     x-----|

     y

  temp


The next step is to point y to x. Draw your arrow to the same location that x points to:

example--> NODE[str="Hello",next]-->NODE[str="open",next]-->NODE[str="leave",next]-->NODE[str="never",next]-->NODE[str="radio",next]-->NODE[str="table",next]-->null

           ^^

     x-----||

     y------|

  temp


And you continue to follow the directions until you can work out what it is the function is trying to do. The next step is the 'while' loop. While y is not null, and the character in x's "str" at position strlen(x->str)-1 is not equal to the character in y's "str" at position 0, continue to loop.

I could go on but it would be beneficial for you to experience the exercise of working through the algorithm yourself. Try it, and see what you can learn.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
engti

engti

    Newbie

  • Members
  • Pip
  • 4 posts
Hello friend.
First, Thanks about this information, It's very helpful to me.

I am not understand this command:
In first start functions;
y = x->next; 

why y is not equal "open"? x="hello", and x->next is equal "open"?
Thank again friend.

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
After the y=x->next; command, y would point to the same thing that x->next points to, like this:

example--> NODE[str="Hello",next]-->NODE[str="open",next]-->NODE[str="leave",next]-->NODE[str="never",next]-->NODE[str="radio",next]-->NODE[str="table",next]-->null

           ^                        ^

     x-----|                        |

     y------------------------------|

So y would point to the node which contains the string "open", assuming x was pointing to the first node.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
engti

engti

    Newbie

  • Members
  • Pip
  • 4 posts
Ok, I am trying again now to solve this question.
You can please write the final answer to question? because i have not have a final question.
thank you again. :thumbup1:

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Is this a homework assignment of yours? My answering the final question would rob you of the learning experience of figuring it out yourself.

If you're stuck on a step and can't figure out what it does, tell me which step and show me your pointer diagram thus far, and I'll guide you through it.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users