Riddle

This is a discussion on Riddle within the C Programming forums, part of the General Programming Boards category; Here is some code that operates on a singly linked list. Say P is pointer to a linked list. Linked ...

  1. #1
    Unregistered
    Guest

    Talking Riddle

    Here is some code that operates on a singly linked list. Say P is pointer to a linked list. Linked list node has value and next elements.


    Q=P->next;
    P->next=NULL;
    while (!Q)
    {
    S = Q;
    Q=Q->next;
    S->next = P;
    P=S;
    }

    at the end of the code what is the value of P. what is the end result.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    11

    Question

    The value of P remains unchanged, because the code segment in braces in never executed due to this statement:

    while (!Q)


    That's the way I see it, anyway. Let me know if you need more help with homework problems. For extra credit, mention to your instructor that he forgot to free the memory he had allocated to the node P->next.

  3. #3
    Unregistered
    Guest

    Riddle - correctoin

    In the above code snippet changed the While statement.


    Q=P->next;
    P->next=NULL;
    while (Q)
    {
    S = Q;
    Q=Q->next;
    S->next = P;
    P=S;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vtables riddle.
    By eXeCuTeR in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2008, 02:57 PM
  2. Complicated riddle?
    By eXeCuTeR in forum C Programming
    Replies: 20
    Last Post: 11-21-2007, 06:55 AM
  3. riddle me this, batman
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-25-2002, 05:19 PM
  4. Pointer Riddle that is bugging me
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-30-2001, 12:11 AM
  5. Another riddle tread!
    By minime6696 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-24-2001, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21