0

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 Answers1

2

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);

cse
  • 4,066
  • 2
  • 20
  • 37