Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Parse error?

Reply
Thread Tools

Parse error?

 
 
Albert
Guest
Posts: n/a
 
      01-03-2010
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
 
Reply With Quote
 
 
 
 
Beej Jorgensen
Guest
Posts: n/a
 
      01-03-2010
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
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-03-2010
Albert <(E-Mail Removed)> 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
 
Reply With Quote
 
Albert
Guest
Posts: n/a
 
      01-03-2010
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
Parse Text File and Output to File John M. Lembo Perl 0 08-01-2003 04:34 PM
Parse an xml file with line breaks in the beginning Raj Mudaliar Perl 0 07-14-2003 06:00 PM
[TABLE NOT SHOWN] problem with HTML::Parse Mitchua Perl 3 07-13-2003 11:38 PM



Advertisments