请问下为什么动态分配的语句处报一下错误:
implicite declaration of function 'int malloc(...)'
/*
已知一个包含有n个节点的单项环形链表,
编写函数,查找链表中num成员数值最小的
节点,输出该结点的num成员。
*/
#include <string.h>
#include <stdio.h>
struct node{
int num;
struct node *next;
}*head;
int search(struct node *p){
int min = p->num;
while(p->next != head){
if((++p)->num < min)
min = p->num;
}
return min;
}
int main(){
int n = 0,num;
struct node *p1,*p2;
p1 = p2 = (struct node*)malloc(sizeof(struct node));
scanf("%d",&p1->num);
while(p1->num != 0){
n++;
if(n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = (struct node*)malloc(sizeof(struct node));
scanf("%d",&p1->num);
}
p2->next = head;
num = search(head + 1);
printf("%d\n",num);
return 0;
}
缺少头文件。。。,,,,,,,
#include
另外你while都没有个限制,输出的最小值都是0
加上 #incude <stdlib.h>
while(p->next != head)这句没弄懂,这是把下一节点的指针变量与一个类型做判断吗?
#include要加上
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int num;
struct node *next;
}*NODE;
NODE head;
int search(NODE p){
int min = p->num;
//printf("%d*\n",min);
while(p->next != head){
if(p->num < min){
min = p->num;
}
p = p->next;
}
printf("%d**\n",min);
return min;
}
void show(NODE n){
while(n->next != head){
printf("%d*",n->num);
n = n->next;
}
}
int main(){
int n = 0;
NODE p1,p2;
p1 = p2 = (NODE)malloc(sizeof(NODE));
head = p1;
scanf("%d",&p1->num);
while(p1->num != 9999){
n++;
if(n == 1) p2 = head;
else{
p1 = (NODE)malloc(sizeof(NODE));
scanf("%d",&p1->num);
p2->next = p1;
p2 = p1;
p2->next = head;
}
}
//show(head);
search(head);
return 0;
}