Thread: A struct problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    A struct problem

    Code:
    #include <stdio.h>
    
    struct Node
    {
    int index;
    struct Node *next_in_line;
    };
    
    void print_list_in_reverse(struct Node * first_in_list);
    
    int main(void)
    { 
    struct Node n1={12,NULL};
    struct Node n2={43,&n1};
    struct Node n3={32,&n2};
    struct Node n4={24,&n3};
    
    print_list_in_reverse(&n4);
    
    return 0;
    }
    void print_list_in_reverse(struct Node * p)
    {
    if(p == NULL)
    return;
    print_list_in_reverse(p->next_in_line);
    printf("%i\t", p->data);
    }

    I don't get the "print_list_in_reverse" function ..
    would anyone explain please?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    First off: do you see how your Nodes are connected together?

    If so, sit down with a piece of paper and trace out what happens. Note that *p is local to the function, so that each time you call the function p is different. The first time *p is n4; the second time *p is n4.next_in_line, which is n3; then it is n3.next_in_line which is n2, etc.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    I understand how to type the list normally ..but not in reverse

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Salahuddin View Post
    I understand how to type the list normally ..but not in reverse
    So read what's there: it's telling you how to print the list in reverse. But you do have to read it.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    I'm sorry but i understand the mechanism but i don't understand the syntax

    What does this code means ?
    Code:
    if(p == NULL)
    return
    And how can i use the function in it .. while defining it ?
    Code:
    print_list_in_reverse(p->next_in_line);

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Salahuddin View Post
    I'm sorry but i understand the mechanism but i don't understand the syntax

    What does this code means ?
    Code:
    if(p == NULL)
    return
    What do you find confusing about this piece of code? It's an if statement.
    Quote Originally Posted by Salahuddin View Post
    And how can i use the function in it .. while defining it ?
    Code:
    print_list_in_reverse(p->next_in_line);
    Look up recursion then.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    ok .. it's an if statement ..
    what does "return;" means?
    I'm a beginner so calm down ..
    Thanks

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It means "return". It returns a value (or in this case no value, since it's a function-returning-nothing) to the function that called it.

    PS: Beginner isn't an excuse for lazy.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    So if (p==NULL) , the function won't continue but it will break right ??
    it will not compile >>
    print_list_in_reverse(p->next_in_line);
    so we can write instead
    Code:
    if (p==NULL)
    return;   //don't continue
    else
    print_list_in_reverse(p->next_in_line);
    printf("%i\t", p->data);
    i hope i got it ...
    PS: Beginner isn't an excuse for lazy.
    I agree with you but i think i'm doing my best to understand this simple code for you ..
    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct struct struct problem....please....help!!!
    By nullifyed in forum C Programming
    Replies: 5
    Last Post: 06-19-2010, 08:19 AM
  2. struct problem
    By eklavya8 in forum C Programming
    Replies: 6
    Last Post: 06-07-2009, 12:23 PM
  3. Struct problem
    By Matty_Alan in forum C Programming
    Replies: 5
    Last Post: 11-19-2008, 03:49 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Problem about struct
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-19-2001, 12:15 PM