getting compile time error in C code of linkedList? plz help me to find compile time error

i am new to c programming, needed help to find compile time error to linkedlist implemntation…
#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct Node{
int value;
struct Node *next;
}

void addFirst(int value,struct Node **head){
struct Node *node=malloc(sizeof(Node));
(*node).value=value;
if(*head==NULL){
(*head)=node;
(**head).next=NULL;
return;

}
(*node).next=*head;
(*head)=node;

}

void traversNode(struct Node *temp){
if(temp==NULL){
printf("%s","LinkedeList Is Empty\n");
return;
}
while(temp!=null){
printf("%d",temp.value);
temp=(*temp).next;
}

}
void main(){

struct Node *head=null;
   clrscr();
traverseNode(head);
addFirst(5,&head);
traverseNode(head);
addFirst(10,&head);
getch();
}

Below code compiles:

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

struct Node{
	int value;
	struct Node *next;
};

void addFirst(int value,struct Node **head){
	struct Node *node= (Node*)malloc(sizeof(Node));
	(*node).value=value;
	if(*head==NULL){
	(*head)=node;
	(**head).next=NULL;
	
		return;
	}

	(*node).next=*head;
	(*head)=node;
}

void traverseNode(struct Node *temp){
	if(temp==NULL){
		printf("%s","LinkedeList Is Empty\n");
		return;
	}
	while(temp!=NULL){
		printf("%d",temp->value);
		temp=(*temp).next;
	}
}

int main(){
	struct Node *head = NULL;
   	//clrscr();
	traverseNode(head);
	addFirst(5,&head);
	traverseNode(head);
	addFirst(10,&head);
	getch();
	
	return 0;
}

I feel lazy to type every bits and pieces. Please compare your code with mine to get the idea.

Brief points:

  • traverseNode() - there was a typo or function definition mismatch.
  • You need to type case the (void*) returned from malloc.
  • On some compiler, malloc is defined in stdlib or in string.h (alloc.h) doesn’t work on Windows devc++, if this is your favorite editor.
  • use NULL instead of null
  • Dot “.” operator is only applicable when you are accessing member of a struct object and not via a pointer. You were trying to access the member of a struct via its pointer, so use “->” arrow operator.
1 Like