0

In the following code for a linked list implementation, I am getting the error cannot convert node_type to node * for argument 1 to 'node *insert(node *)'. I don't understand this message. Basically the program is not able to call the function insert from main().

Can anyone help explain this?

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

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

typedef struct node_type node;

node  *insert(node *head);
void print1(node *temp);

int main(void){
    int dat;
    char x,ch;

    node *temp;


    temp=NULL;
    printf("do u wanna enter a new node? \n");
    scanf("%c", &x);
    if (x=='y' or x=='Y'){

        temp=(node *)malloc(sizeof(node));
        printf("enter the data: \n");
        scanf("%d ", &dat);

        temp->data= dat;
        temp->next = NULL;
    }

    printf("do u want to insert another element?\n");
    scanf("%c ", &ch);
    if( ch=='y' or ch=='Y'){
        insert(temp);
    }
    print1(temp);

    getch();

}
node *insert(node *temp)
{
    int dat;
    char ch1;
    node *newnode;

    newnode=(node *)malloc(sizeof(node));

    printf("enter the data: ");
    scanf("%d", &dat);
    newnode->data=dat;
    newnode->next=temp;
    temp=newnode;

    printf("do u want to insert another element?\n");
    scanf("%c ", &ch1);
    if( ch1=='y' or ch1=='Y'){
        insert(temp);
    }
    else return temp;

}
void print1(node *temp)
{
    int t;

    while(temp!= NULL){
        t= temp->data;
        temp= temp->next;
        printf(" %d ", t);
    }
}
1
  • 1
    In strict C, the structure definition won't compile. If compiled with a C++ compiler, it will compile. Since you're using <conio.h>, you're probably using MS Visual C++, and I'd guess your code is being compiled as C++ rather than C. Commented Feb 27, 2014 at 5:23

2 Answers 2

1

A few problems in your code:

  1. The definition of struct node_type

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

    is not right according to C syntax, type node_type does not exist before the typedef struct node_type node; statement.

    To fix this, you could

    a) define struct node_type like this

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

    or

    b) use (Thanks @yongzhy)

    typedef struct node_type
    {
        int data;
        struct node_type *next;
    };
    

    or

    c) compile your code with a C++ compiler. (Thanks @JonathanLeffler)

  2. All these

    if (x=='y' or x=='Y'){
    

    should be replaced by

    if (x=='y' || x=='Y'){
    

    or you could just include <iso646.h>, which includes #define or || in it (Thanks @JonathanLeffler),

    or you should compile your code with a C++ compiler.

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

1 Comment

Or the code should include <iso646.h> so that or etc are handled (defined: #define or || or equivalent).
0

add to @leeduhem

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

this will void additional line of

typedef struct node_type node;

Comments

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.