0

I am using the following code but its showing error : expected constructor, destructor or type conversion before * token. Its showing the error in function prototype and the function declaration lines.

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

node *insert(node *head);    // the compiler is showing error here

main()
{
  int dat;
  char x,ch;
  struct node_type
  {
    int data;
    node_type *next;
  };

  typedef struct node_type node;

  //   node *head;
  node *temp;

  // head= NULL;
  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);
  }

  getch();

}

node *insert(node *temp)
{
  int dat;
  char ch;
  node *new;

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

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

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

}

what is the error and how do i rectify it?

6
  • It's int main(void) at least, please! Commented Feb 25, 2014 at 17:37
  • 1
    Where is "node" defined? Have you included its header file? Commented Feb 25, 2014 at 17:38
  • You might like to fully quote the error message(s) you get. Commented Feb 25, 2014 at 17:38
  • Is there any particular reason you don't just use std::list? Commented Feb 25, 2014 at 17:42
  • This doesn't look like C++. Commented Feb 25, 2014 at 17:44

2 Answers 2

1

First of all, you can move the definition of node_type out of your main function, above the declaration of the insert function. The error is because node is unknown to the compiler at the point it first sees it. So have something like this at the top:

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

typedef struct node_type node;

node* insert(node *head);

That should take care of the problem you asked about, but there are a few others you need to take care of as well:

  1. You shouldn't be dereferencing the parameter when you call insert, since the function expects a pointer. So for example, you should have this instead:

    if ( ch=='y' or ch=='Y')
    {
       //insert(*temp);
       insert(temp);
    }
    
  2. In insert, you have a node* named new, but new is actually a keyword that shouldn't actually be used to name a variable. You might rename it to something else and it might look like:

    // new above was replaced with newNode
    node *newNode;
    
    newNode=(node *)malloc(sizeof(node));
    
    printf("enter the data: ");
    scanf(" %d ", &dat);
    newNode->data=dat;
    newNode->next=temp;
    temp=new;
    

Hopefully, that should get you to a point where you can compile and test your program.

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

1 Comment

Yeah i got it. The primary reason was using new as variable name. The code is now working. i really appreciate your help.
1

for C

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

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

node *insert(node **head);

node *new_node(void){
    int dat;
    node *new=(node *)malloc(sizeof(node));

    printf("enter the data: ");
    scanf(" %d", &dat);
    new->data = dat;
    new->next = NULL;
    return new;
}

void print(node *np){
    while(np){
        printf("%d ", np->data);
        np = np->next;
    }
    printf("\n");
}

int main(){
    int dat;
    char ch;

    node *temp = NULL;

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

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

node *insert(node **temp){
    int dat;
    char ch;
    node *new = new_node();

    new->next=*temp;
    *temp=new;

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

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.