0
#include<stdio.h>
#include<conio.h>
struct Node{
    int number;
    struct Node * next;
};

struct Node * insertNodeInLinkedList(int number,struct Node * startOfList){
    if(startOfList == NULL){
        startOfList = (struct Node *)malloc(sizeof(struct Node));
        startOfList->number = number;
        startOfList->next = NULL;
    }else{
        struct Node * temporaryNode = startOfList;
        struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
        while(temporaryNode->next != NULL){
            temporaryNode = temporaryNode->next;
        }

        newNode->number = number;
        newNode->next = NULL;
        temporaryNode->next = newNode;

    }
    return startOfList;
}

void display(struct Node * startOfList){
    struct Node * temporaryNode = startOfList;
    while(temporaryNode != NULL){
        printf("%d",temporaryNode->number);
        temporaryNode = temporaryNode->next;
    }   
}

int main (void){
    int howManyNodes = 0;
    int counter = 0;
    int enteredNumber = 0;
    struct Node * startOfMyList = NULL;

    printf("How many nodes do you want in your linked list?");
    scanf("%d",&howManyNodes);

    while(counter < howManyNodes){
        printf("Enter number: ");
        scanf("%d",&enteredNumber);
        startOfMyList = insertNodeIntoLinkedList(enteredNumber,startOfMyList);
        counter++;
    }
    display(startOfMyList);
    getch();
    return 0;
}  

This is my simple program to insert and display nodes of a linked list. However, the line:

startOfList = (struct Node *)malloc(sizeof(struct Node));  

gets flagged as an error. I do not know why.

What is going wrong here ?

2
  • @OliCharlesworth oh the error changed now and I get E:\DevCProjects\DataStructures\SingleLinkedList.o SingleLinkedList.c:(.text+0x12d): undefined reference to insertNodeIntoLinkedList'` Commented Sep 14, 2013 at 8:28
  • Do you have a function called insertNodeIntoLinkedList? Commented Sep 14, 2013 at 8:30

1 Answer 1

1

Your malloc seems to be correct.

For your new error it is simply because in main you are calling insertNodeIntoLinkedList and you define the function before:

insertNodeInLinkedList
//        ^^

Also, you need to include <stdlib.h> for the malloc function.

Sign up to request clarification or add additional context in comments.

4 Comments

ouch ! That was very clumsy of me. I also have this warning 13 32 E:\DevCProjects\DataStructures\SingleLinkedList.c [Warning] incompatible implicit declaration of built-in function 'malloc' [enabled by default] why is that ?
@LittleChild You need to include <stdlib.h>
Sorry for such a dumb question. I do not code in C. I am doing this as a part of my assignment :)
@LittleChild Not a problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.