【发布时间】:2017-06-12 04:49:24
【问题描述】:
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
int key;
struct node *next,*ptr;
};
struct node *head = NULL;
void input(){
ptr=(struct node *)malloc(sizeof(struct node));
for(int i=0;i<5;i++){
printf("enter the elements");
scanf("%d",&ptr->data);
ptr->key=i;
ptr=ptr->next;
}
void display(){
ptr=head;
while(ptr!=NULL){
printf("the linkedlist is --\n");
printf("%d",ptr->data);
ptr=ptr->next;
}
}
}
void main(){
input();
display();
}
这给出了两个错误-
- 错误:“ptr”未声明
- 警告:函数“display”的隐式声明
我已经声明了 ptr ,那为什么会出现这个错误呢? 那么显示功能呢?我的代码逻辑在链表中输入元素并显示它们是否正确?
【问题讨论】:
-
我对您的代码进行了格式化,因此不正确的缩进有望像大拇指一样突出。无论如何,唯一的
ptr是struct node的成员,所以所有ptr = ...都是错误的。如果你没有,get a good book on C.
标签: c linked-list