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

新人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 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印