为什么这个程序用VC6.0编译无法通过?

yoshikis 2011-08-27 09:31:11
#include <stdlib.h> /*包含ma l l o c ( ) 的头文件*/
#include <stdio.h>

struct node /*链表节点的结构*/
{
int num;
struct node *next;
};
void main ( )
{
struct node *creat(); /*函数声明*/
void print();
struct node *head; /*定义头指针*/
head=NULL;/*建一个空表*/
head=creat(head);/*创建单链表*/
print(head);/*打印单链表*/
}
/******************************************/
struct node *creat(struct node*head)/*函数返回的是与节点相同类型的指针*/
{
struct node*p1,*p2;
p1=p2=(struct node*)malloc(sizeof(struct node));/*申请新节点*/
scanf("%d",&p1->num);/*输入节点的值*/
p1->next=NULL;/*将新节点的指针置为空*/
while(p1->num>0)/*输入节点的数值大于0*/
{
if(head==NULL)head=p1;/*空表,接入表头*/
else (p2->next)=p1;/*非空表,接到表尾*/
p2=p1;
p1=(struct node*)malloc(sizeof(struct node));/*申请下一个新节点*/
scanf("%d",&p1->num);/*输入节点的值*/
}
return head;/*返回链表的头指针*/
}
/*******************************************/
void print(struct node*head)/*输出以head为头的链表各节点的值*/
{
struct node *temp;
temp=head;/*取得链表的头指针*/
while(temp!=NULL)/*只要是非空表*/
{
printf("%6d",temp->num);/*输出链表节点的值*/
temp=temp->next;/*跟踪链表增长*/
}
}




C:\Documents and Settings\wgm\sdfw3er.cpp(15) : error C2660: 'creat' : function does not take 1 parameters
C:\Documents and Settings\wgm\sdfw3er.cpp(16) : error C2660: 'print' : function does not take 1 parameters
执行 cl.exe 时出错.
...全文
127 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoshikis 2011-08-27
  • 打赏
  • 举报
回复
感谢各位大侠的帮助,已解决。
programer.M 2011-08-27
  • 打赏
  • 举报
回复
struct node *creat(); 你的函数实现在哪儿?声明->调用head=creat(head),格式不一样。建议你重新整理下你的文件,有点乱;

下面的格式是一致的。

你是不是就放在一个文件(CPP文件)里,如果在一个文件里,就要把函数声明和实现写在main函数上面。否则main函数调用不认识自定义的函数。
chaoliu1024 2011-08-27
  • 打赏
  • 举报
回复
struct node /*链表节点的结构*/
{
int num;
struct node* next;
};

struct node* creat(struct node* head); // 函数声明放在外面,并且补齐参数
void print(struct node* head); // 同上

int main(void)
{

struct node* head; /*定义头指针*/
head = NULL; /*建一个空表*/
head = creat(head); /*创建单链表*/
print(head);/*打印单链表*/

return 0;
}
tsh185 2011-08-27
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

typedef struct _job{
struct _job *next;
int num;
} job;

static job *jhead = NULL, *jtail = NULL;

void job_add(int num){
job *j ;
for (j = jhead; j != NULL; j=j->next)
if (j->num == num)
return ;
if ((j = (job*) malloc(sizeof (job))) == NULL)
return;
j->next = NULL;
j->num = num;

if (jhead == NULL)
jhead = j;
else
jtail->next = j;
jtail = j;
}

int job_run(void) {
job *j, *jn;
int run = 0;

for (j = jhead; j; j = jn) {
printf("j->num = %d\n",j->num);
jn = j->next;
free(j);
run++;
}
jhead = jtail = NULL;
return (run);
}


int main(int argc , char* argv[])
{
int i;
int run;
for (i = 0; i < 100; i++)
{
job_add(i);
}
run = job_run();
printf("end run = %d\n",run);
}
chaoliu1024 2011-08-27
  • 打赏
  • 举报
回复
函数申明放在main()函数的外面。
叶落寒山 2011-08-27
  • 打赏
  • 举报
回复
Creat、printf函数在main函数前面没有声明,导致在main函数中无法使用。

struct node /*链表节点的结构*/
{
int num;
struct node *next;
};

struct node *creat(struct node*head)/*函数返回的是与节点相同类型的指针*/
void print(struct node*head)/*输出以head为头的链表各节点的值*/


void main ( )
{


昵称很不好取 2011-08-27
  • 打赏
  • 举报
回复
函数声明和实现不一样
luciferisnotsatan 2011-08-27
  • 打赏
  • 举报
回复
前置声明struct node *creat(); 没有参数,把参数不上

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧