#include <stdio.h>
#include <stdlib.h>
void insertion(int X,Node *l);
void display( Node *y);
void display(Node *y)
{
printf("n Your Node Contains n Data : %d t Pointer : %d n ",y->data,y->sp);
}
void insertion(int X,Node *l)
{
l->data=X;
l->sp=NULL;
}
typedef struct
{
int data;
int *sp;
}Node;
int main(void)
{
int kl; Node *j;
printf("n Enter the data you like to put :");
scanf("%d",&kl);
insertion(kl,j);
display(j);
return 0;
}
编译过程中出现错误-第3,4,5,9行出现"Unknown Type Node"。为什么?我无法改正这些错误。我最近才开始数据结构,这只是一个节点数据插入程序。
修复非常简单,您必须在尝试使用它之前定义您的结构。
#include<stdio.h>
#include<stdlib.h>
typedef struct // You cant use the Node keyword before this
{
int data;
int *sp;
}Node;
void insertion(int X,Node *l);
void display( Node *y);
void display(Node *y)
{
printf("n Your Node Contains n Data : %d t Pointer : %d n ",y->data,y->sp);
}
void insertion(int X,Node *l)
{
l->data=X;
l->sp=NULL;
}
int main(void)
{
int kl; Node *j;
printf("n Enter the data you like to put :");
scanf("%d",&kl);
insertion(kl,j);
display(j);
return 0;
}
当编译器试图编译
void insertion(int X,Node *l);
它不知道Node
的任何信息。您必须将Node
的声明移到该行之前。
移动行
typedef struct
{
int data;
int *sp;
}Node;
如果您使用Node *j作为链表类型,那么您需要为它分配内存,然后只有您可以为它分配任何值。否则它只是一个指向其他变量的指针
首先指定使用结构的节点
使用数据结构的首要规则是在程序的顶部定义数据结构。然后只有你可以在程序的任何地方使用它。示例程序如下:
using namespace std;
#include<iostream>
struct *node{
int data;
node *link;}top; //top object created
遵循此方法以最小化代码中的错误