You are not logged in. Please login at www.codechef.com to post your questions!

×

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

asked 27 Jun '17, 22:24

hemant_dhanuka's gravatar image

3★hemant_dhanuka
2898
accept rate: 5%

edited 27 Jun '17, 22:27


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.
link

answered 27 Jun '17, 23:11

utkalsinha's gravatar image

5★utkalsinha
723118
accept rate: 12%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,368
×57

question asked: 27 Jun '17, 22:24

question was seen: 258 times

last updated: 27 Jun '17, 23:11