user485498
user485498

Reputation:

C LinkedList example doesn't compile

The following C code is my own way of writing a primitive linked list. It uses a struct called lnode. I know this is not the best/most efficient way to do it but my idea is this: create the base node, use an "iterator" pointer, here q, that points to that last node in the list and then add a new node.

The following code will not compile. I can't find the cause but it hates this line

struct lnode *q= malloc(sizeof(struct lnode));

Any advice on making this idea work? Thanks in advance.

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

struct lnode{
    int value;
    struct lnode *nextnode;

};

int main(){

    struct lnode *startnode = malloc(sizeof(struct lnode));
    startnode->value=0;
    startnode->nextnode=NULL;

    struct lnode *q= malloc(sizeof(struct lnode));
    
    int i = 0;
    
    for(i=0;i<10;i++){
        
        struct lnode *p  = malloc(sizeof(struct lnode));
        p= q->nextnode;
        p->value=i;
        p->nextnode=NULL;
        q=p;
        
    
    }

    
return 0;


}

I would like to point out that I'm a novice. I'm using the Watcom compiler (Why? My computer is old and its all I need for these practice porgrams) The log output is

structure1.c(17): Error! E1063: Missing operand structure1.c(17):

Warning! W111: Meaningless use of an expression structure1.c(17):

Error! E1009: Expecting ';' but found 'struct' structure1.c(17):

Error! E1011: Symbol 'lnode' has not been declared structure1.c(17):

Error! E1011: Symbol 'q' has not been declared structure1.c(17):

Error! E1014: Left operand must be an 'lvalue' structure1.c(19):

I followed the advice given and changed the code the new code is this:

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

struct lnode{
    int value;
    struct lnode *nextnode;

};

int main(){

    struct lnode *startnode = (struct lnode *)malloc(sizeof(struct lnode));
    struct lnode *q;
    startnode->value=0;
    startnode->nextnode=NULL;

    q = malloc(sizeof(struct lnode));

    
    doLoop(q);

return 0;


}

void doLoop(struct lnode *q){

    int i = 0;

    for(i=0;i<10;i++){

        struct lnode *p  = (struct lnode *)malloc(sizeof(struct lnode));
        q->nextnode=p;
        p->value=i;
        p->nextnode=NULL;
        printf("%i, %i\n",p->value,q->value);
        q=p;

    }
}

I printed the "value" values of each node in the list along with the previous value. It works except the first iteration which gives a weird output.

Upvotes: 2

Views: 484

Answers (3)

Morpfh
Morpfh

Reputation: 4093

The error messages is due to the mixing of code and declarations.

Further; You switch p and q around in the for loop.

p = q->next_node; /* here you set p to an undefined area. 
                   * q->next_node is not malloc'd */
p->value = i;     /* here you cause undefined / erronous behaviour
                   * Most probably a SIGSEGV */

So to sum it up, perhaps something like:

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

struct lnode{
    int value;
    struct lnode *nextnode;
};

int main(void)
{

    struct lnode *startnode;
    struct lnode *p;
    size_t z;
    int i;

    z = sizeof(struct lnode);

    if ((startnode = malloc(z)) == NULL) {
        fprintf(stderr, "Unable to malloc %d bytes.\n", z);
        return 1;
    }

    /* Fill list */   
    p = startnode;
    for (i = 0; i < 10; i++) {
        if ((p->nextnode = malloc(z)) == NULL) {
            fprintf(stderr, "Unable to malloc %d bytes.\n", z);
            return 1;
        }

        p->value = i;
        p = p->nextnode;
        p->nextnode = NULL;
    }

    /* Print values */    
    p = startnode;
    while (p->nextnode != NULL) {
        printf("value: %2d\n", p->value);
        p = p->nextnode;
    }

    /* Free */
    p = startnode;
    while (p != NULL) {
        p = p->nextnode;
        free(startnode);
        startnode = p;
    }

    return 0;
}

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258558

The code compiles - http://ideone.com/j6fGe - but the logic is wrong:

 struct lnode *p  = (struct lnode *)malloc(sizeof(struct lnode));
 p= q->nextnode;

Besides the fact that you have a memory leak, I'm sure this is not what you intended.

q->nextnode doesn't point to a valid node, just some random memory. Which you then try to overwrite with p->value=i;.

Upvotes: 1

hmjd
hmjd

Reputation: 121961

I suspect the compiler (Microsoft compilers for example) supports C89 standard only, which does not permit the intermingling of code and declarations. Move declaration of q to top of scope:

int main(){

    struct lnode *startnode = (struct lnode *)malloc(sizeof(struct lnode));
    struct lnode *q
    startnode->value=0;
    startnode->nextnode=NULL;

    q = malloc(sizeof(struct lnode));

Upvotes: 4

Related Questions