如何使用函数在链表中插入元素
How do i insert an element in the linked list using a function?
>我正在使用以下代码,但它显示错误:预期的构造函数,析构函数或*标记之前的类型转换。它显示了函数原型和函数声明行中的错误。
#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;
}
错误是什么,我该如何纠正它?
首先,您可以将 node_type
的定义移出 main 函数,位于 insert
函数的声明上方。该错误是因为编译器在第一次看到节点时不知道它。所以在顶部有这样的东西:
struct node_type
{
int data;
node_type *next;
};
typedef struct node_type node;
node* insert(node *head);
这应该可以解决您提出的问题,但是您还需要处理其他一些问题:
调用
insert
时不应该取消引用参数,因为该函数需要一个指针。因此,例如,您应该拥有以下内容:if ( ch=='y' or ch=='Y') { //insert(*temp); insert(temp); }
在
insert
中,你有一个名为new
的node*
,但new
实际上是一个不应该用来命名变量的关键字。您可以将其重命名为其他名称,它可能如下所示:// 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;
希望这应该让你达到可以编译和测试程序的程度。
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;
}
相关文章:
- 如何在向量数组中插入元素?
- 有没有更快的方法可以在 std::vector 中插入元素
- 映射C++按值排序和插入元素
- 如何在一对集合的向量中插入元素?vector<pair<int,set<string>>>
- 当索引位于末尾和空数组时,向数组插入元素时出现问题
- 在 QSet 中插入元素<QString>并检索 ,删除 QSet 的最后一项
- C++ 在列表末尾插入元素的列表的 STL 向量
- 插入元素后矢量容量增加
- 如何在 c++ 集中插入元素,直到按下 Enter 键
- C++向量插入元素
- 有没有办法在unordered_map的某个位置插入元素
- 重新插入元素可以"re-validate"迭代器吗?
- 内部源代码 - 在链接列表实现中插入元素上的删除会破坏整个应用程序
- 在链表中插入元素
- stl 中是否有任何数据结构可以在 O(1) 或 O(log n) 中插入元素,我可以在其上编写自己的bin_searc
- 如何在将数据结构插入元素后继续迭代数据结构
- 有没有一种方法可以在std::向量的特定位置重复插入元素
- 在向量 (C++) 中保留插入元素的地址
- 插入元素后Unordered_map变空,我做错了什么
- 循环a std :: unordered_map,序列始终是i插入元素的序列