user3195991
user3195991

Reputation: 79

Trying to understand Linked lists and structs

I have put together the following code using only using notes from my lecture, so I apologize if something obvious is missing. I am getting red underlines under the parameters of 'Node *newNode(int item, Node *h)`, which say 'unable to resolve indentifier'. Basically what the code is trying to do is add a new node to the start of the linked list. Can you please tell me what I'm doing wrong?

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

struct Node;
Node *newNode(int item, Node *h);
/*
 * 
 */
int main(int argc, char** argv) {

    typedef struct node{
        int info;
        struct node *link;
    }Node;

    Node *head = NULL;

    Node *newNode(int item, Node *h){

        Node *p;
        *p = (Node *) malloc(sizeof(Node));
        p -> info = item;
        p -> link = h;
        return p;

    }

    head = newNode(1, head);                   //add a new head to start of list




    return (EXIT_SUCCESS);
}

Upvotes: 0

Views: 231

Answers (3)

Persixty
Persixty

Reputation: 8579

There's a subtle difference between a forward declaration of a struct and typedef. It gets confused because its quite common to ignore the struct notation and only make typedefs.

Look at this:

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

struct node; //We're forward declaring a struct called node (small n).

//We're declaring a function that accepts node structs (small n).
struct node *newNode(int item, struct node *h);

//We are doing two things here. 
//First, we're defining the structure of node (which we forward declared).
//Second, we're aliasing struct node (small n) as Node (Big N) in a typedef declaration.
typedef struct node{
    int info;
    struct node *link;
}Node;

//From now on (and only now on) we can refer to Node and it will be seen as 
//the same as struct node (small n).

//Now we define that function we declared above.
//Notice the declaration used struct node (small n) but this just uses Node (big N).
//The typedef tells the compiler they mean the same thing!
Node *newNode(int item, Node *h){

    Node *p;
    p = (Node *) malloc(sizeof(Node));
    p -> info = item;
    p -> link = h;
    return p;
}

//No matter how toy your example we need to clean up after ourselves! 
//It's just good practice.
//This only frees a single node in isolation but it's enough for your example.
void deleteNode(Node* n){
    free(n);
}

int main(int argc, char** argv) {


    Node *head = NULL;


    head = newNode(1, head);                 //add a new head to start of list

    //Do something with your lovely new node here.
    printf("head node value = %d\n",head->info) //The head node value is 1.   

    deleteNode(head);

    return (EXIT_SUCCESS);
}

Upvotes: 1

KillaBytes
KillaBytes

Reputation: 447

You define the structure of the Node within the main program which is not globally defined inside its own scope. You will need to replace struct Node; with the structure defined in main:

typedef struct node{ int info; struct node *link; }Node;

You are also defining the Node function within main and then trying to use it to define your head or starting pointer. Your program should look as follows:

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

typedef struct node{
    int info;
    struct node *link;
}Node;

Node *newNode(int item, Node *h); // Function Prototype to new node.
/*
*
*/
int main(int argc, char** argv) {

 Node *head = NULL;

 head = newNode(1, &head);   //add a new head to start of list // You want to send the reference of the pointer
                                                              // to the function newNode.

 return (EXIT_SUCCESS);
}

Node *newNode(int item, Node *h){

    Node *p;
    p = (Node *)malloc(sizeof(Node)); // Do not use *p that deferences the pointer that you are trying to allocate memory for.
    p->info = item;
    p->link = h;
    return p;
}

A word of warning! I ask that you please reference your textbook or other academic sites that will essentially show you how a program should be formatted. The errors caused within your code are the most basic, and if you wish to fully understand dynamic memory allocation I would ask that you start there before continuing on.

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86381

You're trying to define a function inside of another function. That's not supported by standard C.

You could address that problem by moving the node typedef and newNode() function definition outside of main().

Upvotes: 0

Related Questions