I have the following list-processing interface which I save as .h file.
I don't see anything wrong with it but everytime I compile, it says that the
declaration in line 2 is missing ";"..........can anyone please help?

my code is:

typedef struct node *link;
struct node{ itemType item; link next; };
typedef link Node;
void initNodes(int);
link newNode(int);
void freeNode(link);
void insertNext(link,link);
link deleteNext(link);
link Next(link);
int Item(link);


anyone's help/comments/suggestions will be greatly appreciated.
Thank you.

Care to tell us what itemType is? Compiling eyes want to know.

To be frank, I am new to programming and I am learning C while reading Robert Sedgewick's Algorithms in C (3rd Ed). The code was taken from pg 102. It was mentioned in the book that I should save this code as list.h file so that it can be called on in the future.

I wrote another program that has a #include"list.h".
The structure of the node in my program is
structure node {int item; link next; }. When I compile my program, I get the error message saying that line 2 in list.h is missing a ";".

To be frank, I am new to programming and I am learning C while reading Robert Sedgewick's Algorithms in C (3rd Ed). The code was taken from pg 102. It was mentioned in the book that I should save this code as list.h file so that it can be called on in the future.

I wrote another program that has a #include"list.h".
The structure of the node in my program is
structure node {int item; link next; }. When I compile my program, I get the error message saying that line 2 in list.h is missing a ";".

Paraphrasing code may only lengthen your response time.
Please copy and paste exactly what you have within code tags.

Does the code read as follows?

structure node {int item; link next; }

Or like this?

structure node {int item; link next; }[B];[/B]

In this case, what is link?

And again, how does itemType enter into this?


[edit]Off to find Sedgewick...

Paraphrasing code may only lengthen your response time.
Please copy and paste exactly what you have within code tags.

Does the code read as follows?

structure node {int item; link next; }

Or like this?

structure node {int item; link next; }[B];[/B]

In this case, what is link?

And again, how does itemType enter into this?


[edit]Off to find Sedgewick...

My list.h is as follows:

typedef struct node *link;
struct node { itemType item; link next; };
typedef link Node;
void initNodes(int);
link newNode(int);
void freeNode(link);
void insertNext(link,link);
link deleteNext(link);
link Next(link);
int Item(link);

My program is as follows:
#include<stdlib.h>
#include"list.h"
#include<stdio.h>


main(int argc, char *argv[])
{
int i, N=atoi(argv[1]), M=atoi(argv[2]);
Node t,x;
initNodes(N);

for(i=2, x=newNode(1); 1<=N; i++)
{
t=newNode(i); insertNext(x,t); x=t;}

while (x!=Next(x))
{
for(i=1;i<M; i++) x=Next(x);
freeNode(deleteNext(x));
}

printf("%d\n", Item(x));
}

My list.h is as follows:

typedef struct node *link;
struct node { itemType item; link next; };
typedef link Node;
void initNodes(int);
link newNode(int);
void freeNode(link);
void insertNext(link,link);
link deleteNext(link);
link Next(link);
int Item(link);

My program is as follows:
#include<stdlib.h>
#include"list.h"
#include<stdio.h>


main(int argc, char *argv[])
{
int i, N=atoi(argv[1]), M=atoi(argv[2]);
Node t,x;
initNodes(N);

for(i=2, x=newNode(1); 1<=N; i++)
{
t=newNode(i); insertNext(x,t); x=t;}

while (x!=Next(x))
{
for(i=1;i<M; i++) x=Next(x);
freeNode(deleteNext(x));
}

printf("%d\n", Item(x));
}

I am sorry, I did'nt know how to use the code tag. Now I do...here it is again:

typedef struct node *link;
struct node { itemType item; link next; };
typedef link Node;
void initNodes(int);
link newNode(int);
void freeNode(link);
void insertNext(link,link);
link deleteNext(link);
link Next(link);
int Item(link);

and

main(int argc, char *argv[])
{
        int i, N=atoi(argv[1]), M=atoi(argv[2]);
        Node t,x;
        initNodes(N);

        for(i=2, x=newNode(1); 1<=N; i++)
        {
                t=newNode(i); insertNext(x,t); x=t;}

        while (x!=Next(x))
        {
                for(i=1;i<M; i++) x=Next(x);
                freeNode(deleteNext(x));
                }

        printf("%d\n", Item(x));
        }

Read the code CAREFULLY. Something MUST be defined before it is used... Hence list.h needs some re-arrangement:

struct node { itemType item; link next; }; // Define node first
typedef struct node *link;                  // Link NEEDS node
typedef link Node;                          // Confusing name, Node is a pointer to a node (check caps)
void initNodes(int);
link newNode(int);
void freeNode(link);
void insertNext(link,link);
link deleteNext(link);
link Next(link);
int Item(link);

I personally think link is redundant and useless, as it can be replaced with node* Also if you look at the top you will see that itemType NEEDS a definition somewhere. check back at your book. itemType MAY be a enumeration, meaning if you cant find it, you could replace it with short, int, ect...

>I am learning C while reading Robert Sedgewick's Algorithms in C (3rd Ed).
Please do not follow the example code from that book. The concepts and descriptions are exquisite, but the code SUCKS! It's a poor translation from Pascal, with errors and bad style on every page. You would be better off taking the code examples with a grain of salt and picking up a book such as C Unleashed for examples of good code.

To summarize, Algorithms in C is a great book...provided you don't look at the code.

>Something MUST be defined before it is used
Yes.

>typedef struct node *link; // Link NEEDS node
No, this is a legal forward declaration. node need not be defined first.

>as it can be replaced with node*
struct node*. Because this is C, the struct keyword is required. The primary reason that typedef is used with structures is to avoid this:

typedef struct node {
  /* Stuff */
} node;

struct node *a; /* Legal */
node *b; /* Also legal */

You're thinking of C++.

>check back at your book.
IIRC, itemType is defined like so:

typedef double itemType;

With the qualification that it be changed to suit the needs of whomever uses the code.

This article has been dead for over six months. Start a new discussion instead.