如何使用函数在链表中插入元素

How do i insert an element in the linked list using a function?

本文关键字:插入 元素 链表 何使用 函数      更新时间:2023-10-16

>我正在使用以下代码,但它显示错误:预期的构造函数,析构函数或*标记之前的类型转换。它显示了函数原型和函数声明行中的错误。

#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);

这应该可以解决您提出的问题,但是您还需要处理其他一些问题:

  1. 调用insert时不应该取消引用参数,因为该函数需要一个指针。因此,例如,您应该拥有以下内容:

    if ( ch=='y' or ch=='Y')
    {
       //insert(*temp);
       insert(temp);
    }
    
  2. insert中,你有一个名为newnode*,但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;
}