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

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    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. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by stahta01 View Post
    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.
    Devoted my life to programming...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by GReaper View Post
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    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. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by quzah View Post
    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!
    Devoted my life to programming...

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by engti View Post
    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.
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by GReaper View Post
    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. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by rags_to_riches View Post
    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!
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exam question help: Take 2
    By ÉireKarl in forum C Programming
    Replies: 11
    Last Post: 05-02-2010, 02:12 PM
  2. Exam question help
    By ÉireKarl in forum C Programming
    Replies: 116
    Last Post: 05-02-2010, 12:03 PM
  3. help for my exam. easy question
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-15-2008, 03:09 PM
  4. another exam question
    By rjeff1804 in forum C Programming
    Replies: 4
    Last Post: 02-12-2003, 10:29 PM
  5. exam question
    By teja22 in forum C Programming
    Replies: 2
    Last Post: 10-08-2001, 08:03 PM