Parse error?

Discussion in 'C Programming' started by Albert, Jan 3, 2010.

  1. Albert

    Albert Guest

    Hi,

    Why does GCC returns a parse error on line 42 where I make the root's
    right child/link point to something new?

    The following contains a modified insertion function for AA (Andersson)
    trees. The only links that should be created are those when a value is
    inserted and it is larger than any other value already in the tree.
    Otherwise, what is passed replaces a value already in the tree. Note:
    *no* values passed to this function will be equal.

    Andersson trees are balanced binary search trees that are simpler to
    implement than AVL trees. Split performs a right tree rotation, skew
    performs a left tree rotation and bumps up the new "root" up a level. As
    can be seen, newly added nodes start at level 1. Split and skew are
    called in that order to remove left horizontal links and consecutive
    links respectively and we move up through the "stack" (from recursion :)
    ) calling these functions to fix anything mixed up (in both insert() and
    remove()).

    I've removed the contents of main which initialises nil.

    #include <stdlib.h>

    struct node {
    int value, level;
    struct node *link[2];
    };

    struct node *nil;

    struct node *skew(struct node *root)
    {
    if (root != nil) {
    if (root->level == root->link[0]->level) {
    struct node *save = root;
    root = root->link[0];
    save->link[0] = root->link[1];
    root->link[1] = save;
    }
    root->link[1] = skew(root->link[1]);
    }
    return root;
    }

    struct node *split(struct node *root)
    {
    if (root->level == root->link[1]->link[1]->level && root != nil) {
    struct node *save = root;
    root = root->link[1];
    save->link[1] = root->link[0];
    root->link[0] = save;
    ++root->level;
    root->link[1] = split(root->link[1]);
    }
    return root;
    }

    struct node *insert(struct node *root, int value)
    {
    if (root->link[value > root->value] != nil)
    return root = split(skew(insert(root->link[value > root->value], value)));
    else if (value > root->value) {
    root->link[1] = (struct node *) malloc(struct node);
    root->link[1]->value = value;
    root->link[1]->level = 1;
    root->link[1]->link[0] = root->link[1]->link[1] = nil;
    return root = split(skew(root));
    } else {
    root->value = value;
    return root;
    }
    }

    int main()
    {
    return 0;
    }

    TIA,
    Albert
     
    Albert, Jan 3, 2010
    #1
    1. Advertising

  2. On 01/02/2010 08:05 PM, Albert wrote:
    > root->link[1] = (struct node *) malloc(struct node);


    malloc takes the number of bytes to allocate, not the type to allocate.
    Try:

    malloc(sizeof(struct node));

    HTH,
    -Beej
     
    Beej Jorgensen, Jan 3, 2010
    #2
    1. Advertising

  3. Albert

    Ben Pfaff Guest

    Albert <> writes:

    > root->link[1] = (struct node *) malloc(struct node);


    You want malloc(sizeof(struct node)). I also recommend dropping
    the cast.

    I only looked at this line of your code.
    --
    "Your correction is 100% correct and 0% helpful. Well done!"
    --Richard Heathfield
     
    Ben Pfaff, Jan 3, 2010
    #3
  4. Albert

    Albert Guest

    Beej Jorgensen wrote:
    > On 01/02/2010 08:05 PM, Albert wrote:
    >> root->link[1] = (struct node *) malloc(struct node);

    >
    > malloc takes the number of bytes to allocate, not the type to allocate.
    > Try:
    >
    > malloc(sizeof(struct node));
    >
    > HTH,
    > -Beej


    Thanks.
     
    Albert, Jan 3, 2010
    #4
    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. learning_C++

    error: parse error before `(' token

    learning_C++, Sep 21, 2004, in forum: C++
    Replies:
    4
    Views:
    10,039
    Sharad Kala
    Sep 21, 2004
  2. Vittal
    Replies:
    2
    Views:
    757
    Gordon Burditt
    Jul 8, 2003
  3. Replies:
    19
    Views:
    1,184
    Daniel Vallstrom
    Mar 15, 2005
  4. dkjena1
    Replies:
    1
    Views:
    1,942
    joris
    Jun 8, 2011
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    212
    7stud --
    Feb 20, 2008
Loading...

Share This Page