astronaut0131 2016-10-28 06:29 采纳率: 33.3%
浏览 1061
已采纳

新人C语言编译器报错问题

#include
#include
#define MAXSIZE 10
int G[MAXSIZE][MAXSIZE],Nv,Ne,visit[MAXSIZE]={0},getnext[MAXSIZE]={0},visit_2[MAXSIZE]={0},getnext_2[MAXSIZE]={0};
struct TreeQueue{
int Data;
struct TreeQueue Next;
};
typedef struct {
struct TreeQueue *Front;
struct TreeQueue *Rear;
}Queue;
typedef Queue *Link;
Link CreateQueue()
{
Link Q;
struct TreeQueue *Node=(TreeQueue
)malloc(sizeof(struct TreeQueue));
Node->Next=NULL;
Q->Front=Q->Rear=Node;
return Q;
}

void AddQ(int data,Link Q)
{
struct TreeQueue Node=(TreeQueue)malloc(sizeof(struct TreeQueue));
Node->Data=data;
Node->Next=NULL;
Q->Rear->Next=Node;
Q->Rear=Q->Rear->Next;
}
int DeleteQ(Link Q)
{
TreeQueue* temp;
int ele;
temp=Q->Front->Next;
Q->Front->Next=temp->Next;
ele=temp->Data;
free(temp);
return ele;
}

void BuildGraph()
{
int i,j,v1,v2;
scanf("%d",&Nv);
for(i=0;i {
for(j=0;j {
G[i][j]=0;
}
}
scanf("%d",&Ne);
for(i=0;i {
scanf("%d %d",&v1,&v2);
G[v1][v2]=1;
G[v2][v1]=1;
}
}
void DFS(int v)
{
int i;
visit[v]++;
if(visit[v]==1)
{
printf("%d ",v);
getnext[v]=1;
}
for(i=0;i {
if(G[v][i]==1)
{
if(visit[i]==0)
{
DFS(i);
}
}
}
}
void BFS(int v)
{
Queue *Q=CreateQueue();
visit_2[v]++;
if(visit_2[v]==1)
{
printf("%d ",v);
getnext_2[v]=1;
}
AddQ(v,Q);
while(Q->Rear!=Q->Front)
{
int i;
v=DeleteQ(Q);
for(i=0;i<MAXSIZE;i++)
{
if(G[v][i]==1)
{
if(visit_2[i]==0)
{
visit_2[v]++;
if(visit_2[v]==1)
{
printf("%d ",v);
getnext_2[v]=1;
}
AddQ(i,Q);
}
}
}
}
}
int main()
{
int i;
BuildGraph();
for(i=0;i<Nv;i++)
{
if(getnext[i]==0)
{
printf("{ ");
DFS(i);
printf("}\n");
}
}
for(i=0;i<Nv;i++)
{
if(getnext_2[i]==0)
{
printf("{ ");
BFS(i);
printf("}\n");
}
}
return 0;
}

这是代码 为啥我的编译器疯狂报错TreeQueue没定义??
.c||In function 'CreateQueue':|
.c|17|error: 'TreeQueue' undeclared (first use in this function)|
.c|17|error: (Each undeclared identifier is reported only once|
.c|17|error: for each function it appears in.)|
.c|17|error: expected expression before ')' token|
.c||In function 'AddQ':|
.c|25|error: 'TreeQueue' undeclared (first use in this function)|
.c|25|error: expected expression before ')' token|
.c||In function 'DeleteQ':|
.c|33|error: 'TreeQueue' undeclared (first use in this function)|
.c|33|error: 'temp' undeclared (first use in this function)|
||=== Build finished: 8 errors, 0 warnings ===|

  • 写回答

2条回答 默认 最新

  • 小灸舞 2016-10-28 07:22
    关注

    比如这句:struct TreeQueue Node = (TreeQueue)malloc(sizeof(struct TreeQueue));
    你要写成struct TreeQueue Node = (struct TreeQueue)malloc(sizeof(struct TreeQueue));这样,你少了struct
    PS:CreateQueue函数中应该为Q也malloc空间才行

     #include <stdio.h>
    #include <stdlib.h>
    #define MAXSIZE 10
    int G[MAXSIZE][MAXSIZE], Nv, Ne, visit[MAXSIZE] = { 0 }, getnext[MAXSIZE] = { 0 }, visit_2[MAXSIZE] = { 0 }, getnext_2[MAXSIZE] = { 0 };
    struct TreeQueue{
        int Data;
        struct TreeQueue *Next;
    };
    typedef struct {
        struct TreeQueue *Front;
        struct TreeQueue *Rear;
    }Queue;
    typedef Queue *Link;
    Link CreateQueue()
    {
        Link Q = (Link)malloc(sizeof(Queue));
        struct TreeQueue *Node = (struct TreeQueue*)malloc(sizeof(struct TreeQueue));
        Node->Next = NULL;
        Q->Front = Q->Rear = Node;
        return Q;
    }
    
    void AddQ(int data, Link Q)
    {
        struct TreeQueue *Node = (struct TreeQueue*)malloc(sizeof(struct TreeQueue));
        Node->Data = data;
        Node->Next = NULL;
        Q->Rear->Next = Node;
        Q->Rear = Q->Rear->Next;
    }
    int DeleteQ(Link Q)
    {
        struct TreeQueue* temp;
        int ele;
        temp = Q->Front->Next;
        Q->Front->Next = temp->Next;
        ele = temp->Data;
        free(temp);
        return ele;
    }
    
    void BuildGraph()
    {
        int i, j, v1, v2;
        scanf("%d", &Nv);
        for (i = 0; i<Nv; i++)
        {
            for (j = 0; j<Nv; j++)
            {
                G[i][j] = 0;
            }
        }
        scanf("%d", &Ne);
        for (i = 0; i<Ne; i++)
        {
            scanf("%d %d", &v1, &v2);
            G[v1][v2] = 1;
            G[v2][v1] = 1;
        }
    }
    void DFS(int v)
    {
        int i;
        visit[v]++;
        if (visit[v] == 1)
        {
            printf("%d ", v);
            getnext[v] = 1;
        }
        for (i = 0; i<MAXSIZE; i++)
        {
            if (G[v][i] == 1)
            {
                if (visit[i] == 0)
                {
                    DFS(i);
                }
            }
        }
    }
    void BFS(int v)
    {
        Queue *Q = CreateQueue();
        visit_2[v]++;
        if (visit_2[v] == 1)
        {
            printf("%d ", v);
            getnext_2[v] = 1;
        }
        AddQ(v, Q);
        while (Q->Rear != Q->Front)
        {
            int i;
            v = DeleteQ(Q);
            for (i = 0; i<MAXSIZE; i++)
            {
                if (G[v][i] == 1)
                {
                    if (visit_2[i] == 0)
                    {
                        visit_2[v]++;
                        if (visit_2[v] == 1)
                        {
                            printf("%d ", v);
                            getnext_2[v] = 1;
                        }
                        AddQ(i, Q);
                    }
                }
            }
        }
    }
    int main()
    {
        int i;
        BuildGraph();
        for (i = 0; i<Nv; i++)
        {
            if (getnext[i] == 0)
            {
                printf("{ ");
                DFS(i);
                printf("}\n");
            }
        }
        for (i = 0; i<Nv; i++)
        {
            if (getnext_2[i] == 0)
            {
                printf("{ ");
                BFS(i);
                printf("}\n");
            }
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动