[英]linked list implementation in C
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 *)'
. 在以下用于链表实现的代码中,我得到以下错误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()
. 基本上,程序无法从main()
调用函数insert
。
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);
}
}
A few problems in your code: 您的代码中的一些问题:
The definition of struct node_type
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;
根据C语法是不正确的,在typedef struct node_type node;
之前,类型node_type
不存在typedef struct node_type node;
statement. 声明。
To fix this, you could 要解决此问题,您可以
a) define struct node_type
like this a)像这样定义struct node_type
struct node_type { int data; struct node_type *next; };
or 要么
b) use (Thanks @yongzhy) b)使用(感谢@yongzhy)
typedef struct node_type { int data; struct node_type *next; };
or 要么
c) compile your code with a C++ compiler. c)用C ++编译器编译代码。 (Thanks @JonathanLeffler) (感谢@JonathanLeffler)
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 ||
或者您可以只包含<iso646.h>
,其中包括#define or ||
in it (Thanks @JonathanLeffler), 在其中(感谢@JonathanLeffler),
or you should compile your code with a C++ compiler. 或者您应该使用C ++编译器来编译代码。
add to @leeduhem 添加到@leeduhem
typedef struct node_type
{
int data;
struct node_type *next;
} node;
this will void additional line of 这将使额外的行无效
typedef struct node_type node;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.