【问题标题】:How do i insert an element in the linked list using a function?如何使用函数在链表中插入元素?
【发布时间】:2014-03-28 02:57:10
【问题描述】:

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

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

}

什么是错误以及如何纠正它?

【问题讨论】:

  • 请至少是int main(void)
  • “节点”在哪里定义?是否包含了它的头文件?
  • 您可能希望完整引用收到的错误消息。
  • 有什么特别的原因你不只使用 std::list 吗?
  • 这看起来不像 C++。

标签: c++ c insert linked-list


【解决方案1】:

首先,您可以将node_type 的定义移出您的主函数,位于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;
    

希望这能让您达到可以编译和测试程序的地步。

【讨论】:

  • 是的,我明白了。主要原因是使用 new 作为变量名。该代码现在正在运行。非常感谢您的帮助。
【解决方案2】:

为 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多