【发布时间】:2016-05-14 08:00:00
【问题描述】:
我参加了斯坦福的算法第 1 部分课程,我正在尝试在 c 中实现 快速查找算法。
下面是我实现快速查找的代码。
#include <stdio.h>
#include <stdlib.h>
#define n 10
void union (int, int, int *);
void connected(int, int, int *);
int main(){
//initializing array value to it's index value
int i;
int id[n];
for(i = 0 ; i < n;i++)
{
id[i] = i;
}
union(0,5,id);
union(3,4,id);
union(5,8,id);
connected(3,4,id);
connected(0,8,id);
}
void union(int a, int b,int *p)
{
int x, y;
x = p[b];
y= p[a];
for(int i = 0; i < n; i++)
{
if (p[i]==y)
p[i] = x;
}
}
void connected(int a, int b,int *p)
{
if(p[a]==p[b])
printf("connected");
else
printf("not connected");
}
每当我编译程序时,都会产生以下错误。
我已经检查了我的函数原型和声明,但我找不到问题。
quick_find.c:4:12: error: expected ‘{’ before ‘(’ token
void union (int, int, int *);
^
quick_find.c:4:6: error: two or more data types in declaration specifiers
void union (int, int, int *);
^
quick_find.c: In function ‘main’:
quick_find.c:15:7: error: expected ‘{’ before ‘(’ token
union(0,5,id);
^
quick_find.c:16:7: error: expected ‘{’ before ‘(’ token
union(3,4,id);
^
quick_find.c:17:7: error: expected ‘{’ before ‘(’ token
union(5,8,id);
^
quick_find.c: At top level:
quick_find.c:23:11: error: expected ‘{’ before ‘(’ token
void union(int a, int b,int *p)
^
quick_find.c:23:6: error: two or more data types in declaration specifiers
void union(int a, int b,int *p)
【问题讨论】:
-
现在看看你在 Stack Overflow 上的帖子:所有
unions 都是蓝色的,不像connected是黑色的。深蓝色表示它是语言关键字,与if或while不同。 -
您正在编辑代码并使答案无效。这是非常不友好的。请停止这样做; SO 不是语法/语法删除工具。复制/粘贴您的代码,如果您希望发出编辑信号,请附加修改后的副本或提出其他问题!
-
我会因为拼写错误而关闭它。
-
编辑旨在添加澄清问题的新信息,不改变它。如果您有不同的问题(尤其是代码更改时),请作为新问题提出。像您一样使用编辑来更改问题非常棒,因为它会使问题中的现有答案不同步,这使得未来的观众很难偶然发现这些答案。编辑时请为他人着想。再见...