如何在“graph.c”文件中使用“stack.c”的功能?

时间:2015-08-20 13:07:25

标签: c data-structures graph stack

我正在尝试使用名为“Topoligical Sorting”的算法来绘制图形。

为了使用它,我创建了两个数据结构:图形和堆栈。两者都是分开编写的。

我正在尝试在文件graph.c中使用堆栈数据结构(以及诸如push和pop之类的操作)。为此,我写了:

#include "stack.h"

在graph.c中,当我编译它时,没有错误。

嗯,当我在graph.c中创建堆栈s1时没有编译错误(参见下面的代码):

Stack *s1 = init(); //Creates a stack s1

当我尝试使用堆栈的另一个操作(如push和pop)时出现问题。例如,如果我写:

push(3,s1);

尝试编译它,出现以下错误:

graph.c: In function `DIGRAPHdfs':
graph.c:71: parse error before `int'
graph.c:73: `v' undeclared (first use in this function)
graph.c:73: (Each undeclared identifier is reported only once
graph.c:73: for each function it appears in.)

当我尝试使用pop函数时出现同样的错误。

为什么我会收到此错误编译?

源文件

graph.c

(请参阅此代码中的函数Digraphdfs,出现错误)

#include<stdio.h>
#include<stdlib.h>
#include "graph.h"
#include "stack.h"
#define maxV 10000


static int count, pre[maxV],lbl[maxV];

struct digraph {
 int V; 
 int A;
 int **adj;
};

//Cria a matriz de adjacências (utilizando a técnica de vetor de ponteiros)
int **MATRIXint( int r, int c, int val) {
  Vertex i,j;
  int **m = malloc(r*sizeof(int*));
  for (i = 0; i < r; i++)
       m[i] = malloc(c*sizeof(int));
  for (i = 0; i < r; i++)
       for (j = 0; j < c; j++)
           m[i][j] = val;
  return m;
}


//Cria o digrafo com V vertices e V arcos
Digraph DIGRAPHinit(int V){
   Digraph G = malloc(sizeof *G);
   G->V = V;
   G->A = 0;
   G->adj = MATRIXint(V,V,0);
   return G;
}

//Insere o arco entre v e w
void DIGRAPHinsertA( Digraph G, Vertex v, Vertex w) {
  if (G->adj[v][w] == 0)
      {
    G->adj[v][w] = 1;
    G->A++;
    }
}

//Remove o arco entre v e w
void DIGRAPHremoveA( Digraph G, Vertex v, Vertex w){
  if (G->adj[v][w] == 1){
    G->adj[v][w] = 0;
    G->A--;
  }
}

void DIGRAPHshow(Digraph G){
   Vertex v,w;
   for (v = 0; v < G->V; v++){
     printf("%2d:", v);
      for (w = 0; w < G->V; w++)
         if (G->adj[v][w] == 1)
            printf(" %2d", w);
      printf("\n");
  }

}

void DIGRAPHdfs( Digraph G) {
Stack *s1 = init(); //There's no error when I create a stack
push(3,s1); //I get a compilation error when I try to use the push function (The same for "pop").

Vertex v;
count = 0;
for (v = 0; v < G->V; v++)
     pre[v] = -1;
for (v = 0; v < G->V; v++)
     if(pre[v] == -1)
         dfsR(G,v);

for (v = 0; v< G->V;v++)
     printf("%d ",pre[v]);
printf("\n");
}


void dfsR(Digraph G, Vertex v) {

Vertex w;
pre[v] = count++;
for(w = 0; w < G->V; w++)
     if(G->adj[v][w] != 0 && pre[w] == -1)
          dfsR(G,w);

}

int DIGRAPHreach( Digraph G, Vertex s, Vertex t) {
Vertex w;
for (w = 0; w< G->V; w++)
     lbl[w] = 0;
reachR(G,s);
if (lbl[t] == 0) return 0;
else return 1;
}

void reachR (Digraph G, Vertex v) {

Vertex w;
lbl[v] = 1;
for (w = 0; w < G->V; w++)
  if(G->adj[v][w] == 1 && lbl[w] == 0)
    reachR(G,w);
}

graph.h

#define Vertex int

typedef struct digraph *Digraph;

Digraph DIGRAPHinit(int V);
int **MATRIXint(int r, int c, int val);
void DIGRAPHinsertA(Digraph G, Vertex v, Vertex w);
void DIGRAPHremoveA(Digraph G, Vertex v, Vertex w);
void DIGRAPHshow(Digraph G);
void DIGRAPHdfs( Digraph G);
void dfsR(Digraph G, Vertex v);
void reachR (Digraph G, Vertex v);
int DIGRAPHreach( Digraph G, Vertex s, Vertex t);

stack.c

#include<stdio.h>
#include<stdlib.h>
#include "stack.h"
#define N 5
struct stack {
    int n; //The quantity of elements of the stack
    int vector[5]; //The stacks as a vector.
};
//Initialization of the stack
Stack* init(){
    Stack *s;
    s = (Stack*)malloc(sizeof(Stack));
    if (s == NULL){
        printf("FATAL ERROR!\n");
        exit(1);
    }
    s->n = 0;
    return s;
}
//Push Operation
void push(int q,Stack*s){
    //Checks stack overflow
    if (s->n == N){
        printf("Stack Overflow!\n");
        exit(1);
    }   
    s->vector[s->n] = q;
    s->n++;
}
int pop(Stack*s){
    int v;
    if (s->n == 0){
        printf("There's no element in the stack!\n");
        exit(2);
    }
    s->n--;
    v = s->vector[s->n];

    return v;

}
void print_stack(Stack*s){
    int i;
    for(i = 0; i<s->n; i++){
        printf("vector[%d] = %d \n",i, s->vector[i]);
    }
}

stack.h

typedef struct stack Stack;
Stack* init();
void push(int q,Stack*s);
void print_stack(Stack*s);
int pop(Stack*s);

2 个答案:

答案 0 :(得分:4)

您的编译器似乎是ANSI C编译器。在ANSI C中,所有变量声明必须位于块的顶部。变量声明之间不允许声明。要解决您的错误,请将push()调用移至v声明

下方
void DIGRAPHdfs( Digraph G) {
    Stack *s1 = init(); //There's no error when I create a stack
    Vertex v;
    push(3,s1); //I get a compilation error when I try to use the push function (The same for "pop").

    count = 0;
    for (v = 0; v < G->V; v++)
         pre[v] = -1;
    for (v = 0; v < G->V; v++)
         if(pre[v] == -1)
             dfsR(G,v);

    for (v = 0; v< G->V;v++)
         printf("%d ",pre[v]);
    printf("\n");
}

答案 1 :(得分:2)

一开始,C只允许在块的开头声明变量。您可以更改代码:

void DIGRAPHdfs( Digraph G) {
Stack *s1 = init(); //There's no error when I create a stack
Vertex v; // Declaration moved to before any statements.
push(3,s1); 

或者您可以更改编译以符合以后的标准。对于gcc,默认使用c90标准。如果您添加标记-std=c99 gcc将使用c99标准。如果您使用其他编译器,请查看相应编译器选项的文档。