0
votes

This the error that I get. I'm trying to implement linked lists in c language.

prog.c: In function ‘Insert’: prog.c:33:26: error: ‘node’ undeclared (first use in this function) struct node* temp = (node*)malloc(sizeof(struct node));

the code is as below

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

struct node{
    int data;
    struct node* next;
};

struct node* head;

void Insert(int x);
void Print();

int main(void){

    head = NULL;
    printf("how many numbers?");
    int n,i,x;
    scanf("%d",&n);

    for(i=0;i<n;i++){
        printf("Enter the number");
        sacnf("%d",&x);
        Insert(x);
        Print();
    }

    return 0;
}

void Insert(int x){

    struct node* temp = (node*)malloc(sizeof(struct node));
    temp->data = x;
    (*temp).next = head;
    head = temp;
}

void Print(){

    struct node* temp = head;
    printf("\nThe List is ");
    while(temp!=NULL){
        printf(" %d", temp->data);
        temp=temp->next;
    }
}
1
There is no node. There is struct node. C isn't C++. And stop casting malloc in C programs regardless. - WhozCraig
In C you need to use typedef if you want to use a structure name as a type by itself. - Barmar
that worked.. thanks. - Eshan Pandey

1 Answers

2
votes

The problem is in line struct node* temp = (node*)malloc(sizeof(struct node)); of function void Insert(int x), it should be struct node* temp = (struct node*)malloc(sizeof(struct node));. You can find corrected and working code Here.

Note In line sacnf("%d",&x); of function main(void), it should be scanf("%d",&x);