Not as expected.

Discussion in 'C Programming' started by dmjcunha, Jan 29, 2011.

  1. dmjcunha

    dmjcunha Guest

    Hi guys. I have a simple program and I hope you can help me. The code
    is below. The problem is that I was expecting the second "head->next"
    printf statement not to be nil. Thanks in advance.

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct node *link;
    struct node {
    char charctr;
    link next; };

    static link head = NULL;

    void dputatbeg(char t)
    {
    link x;

    x = malloc(sizeof *x);
    x->charctr = t;
    x->next = head;
    head = x;
    }

    void dputatend(char t)
    {
    link x;

    x = head;
    if(head == NULL)
    head = (x = malloc(sizeof *x));
    else {
    while(x != NULL)
    x = x->next;
    x = malloc(sizeof *x);
    }
    x->charctr = t;
    x->next = NULL;
    }

    int main(void)
    {
    char c;

    if(c = getchar()) {
    dputatbeg(c);
    printf("head %p\t", head);
    printf("head->next %p\n", head->next);
    }

    if(c = getchar()) {
    dputatend(c);
    printf("head %p\t", head);
    printf("head->next %p\n", head->next);
    }

    return 1;
    }
     
    dmjcunha, Jan 29, 2011
    #1
    1. Advertising

  2. dmjcunha

    kid joe Guest

    dmjcunha writes:
    > void dputatend(char t)
    > {
    > link x;
    >
    > x = head;
    > if(head == NULL)
    > head = (x = malloc(sizeof *x));
    > else {
    > while(x != NULL)
    > x = x->next;
    > x = malloc(sizeof *x);
    > }
    > x->charctr = t;
    > x->next = NULL;
    > }


    Hi dmjcunha

    The "else" part should be:

    else {
    while(x->next != NULL)
    x = x->next;
    x->next = malloc(sizeof *x);
    x = x->next;
    }

    ~~ joe


    --


    ( )
    ( )
    G O O D ( ) M O R N I N G ! !
    ( )
    ) )
    ( ( /\
    (_) / \ /\
    ________[_]________ /\/ \/ \
    /\ /\ ______ \ / /\/\ /\/\
    / \ //_\ \ /\ \ /\/\/ \/ \
    /\ / /\/\ //___\ \__/ \ \/
    / \ /\/ \//_____\ \ |[]| \
    /\/\/\/ //_______\ \|__| \
    / \ /XXXXXXXXXX\ \
    \ /_I_II I__I_\__________________\
    I_I| I__I_____[]_|_[]_____I
    I_II I__I_____[]_|_[]_____I
    I II__I I XXXXXXX I
    ~~~~~" "~~~~~~~~~~~~~~~~~~~~~~~~
     
    kid joe, Jan 29, 2011
    #2
    1. Advertising

  3. dmjcunha

    Eric Sosman Guest

    On 1/29/2011 4:07 PM, dmjcunha wrote:
    > Hi guys. I have a simple program and I hope you can help me. The code
    > is below. The problem is that I was expecting the second "head->next"
    > printf statement not to be nil. Thanks in advance.
    >
    > [...]
    > void dputatend(char t)
    > {
    > link x;
    >
    > x = head;
    > if(head == NULL)
    > head = (x = malloc(sizeof *x));
    > else {
    > while(x != NULL)
    > x = x->next;
    > x = malloc(sizeof *x);
    > }
    > x->charctr = t;
    > x->next = NULL;
    > }
    > [...]


    A really good thing to do when you're confused about what's
    happening with a linked data structure is to draw "before" and
    "after" diagrams. This function handles two cases: Either `head'
    is NULL and the list is empty (so the new node becomes the only
    node), or `head' is non-NULL and the list is non-empty. Since
    you've probably figured out that the function works properly for
    the first case, let's draw pictures of the second. You want to
    begin with

    head -> [node1] -> [node2] -> ... -> [nodeN] -> NULL

    .... and and end up with

    head -> [node1] -> [node2] -> ... -> [nodeN] -> [nodeX] -> NULL

    Comparing the arrows in the two diagrams, you can see that two
    changes must occur: The new nodeX must point to NULL, and the old
    nodeN must point to nodeX. Two arrows must change, so the function
    must store new values in two `next' links in nodes. Count the number
    of such stores the function actually performs, and perhaps you'll be
    on your way to seeing what's wrong.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 29, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul
    Replies:
    0
    Views:
    316
  2. Richard Huff
    Replies:
    2
    Views:
    407
    Richard Huff
    Jan 6, 2004
  3. Elaine Jackson

    bitwise not - not what I expected

    Elaine Jackson, Aug 17, 2003, in forum: Python
    Replies:
    12
    Views:
    636
    Dennis Lee Bieber
    Aug 20, 2003
  4. Tim Peters

    RE: bitwise not - not what I expected

    Tim Peters, Aug 17, 2003, in forum: Python
    Replies:
    3
    Views:
    520
    Carl Banks
    Aug 17, 2003
  5. Tim Peters

    RE: bitwise not - not what I expected

    Tim Peters, Aug 18, 2003, in forum: Python
    Replies:
    2
    Views:
    315
    Elaine Jackson
    Aug 19, 2003
Loading...

Share This Page