我有一個程序,給我的錯誤[Error] conflicting types for 'empty'
和[Error] conflicting types for 'full'
。我有一個預感,它與enum bool
的使用有關(這是我第一次嘗試使用它)。我看過其他類似的問題,這些問題對我沒有幫助,因爲問題是忘記在程序中聲明原型。我已經確定在main之前寫入我的函數。enum bool的衝突類型?
這是我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char** data; // array of strings represnting the stack
int top; // -1 if empty
int size;
}Stack;
typedef enum { FALSE, TRUE } bool;
Stack* create(){
Stack *s;
s = (Stack*)malloc(sizeof(Stack));
s->top = -1;
s->size = 10;
s->data = (char**)malloc(s->size*sizeof(char*));
return s;
}
void deleteStack(Stack* ps){
while(ps->top = 0){
free(ps->data[ps->top]);
ps->top--;
}
free(ps->data);
}
void push(Stack* ps, char* str, int* size){ //may need to call realloc bfr completing push
if(full(ps)){
char **temp = realloc(ps->data, ps->size*sizeof(char*));
ps->data == temp;
printf("Stack capacity has grown from %d to %d elements\n", ps->size**size, ps->size**(++size));
}
ps->data[++ps->top] = str;
}
char* pop(Stack* s, int* i){ //returns the string that was removed from the stack
if(empty(s))
return NULL;
printf("#of elements after popping: %d\tstring popped: %s\n", --i, s->data[s->top]);
return s->data[s->top--];
}
bool empty(Stack s){ // returns true if stack has no elements else false
if(s.top == -1)
return TRUE;
return FALSE;
}
bool full(Stack s){ //returns true if no more room else false
if(s.top == s.size-1)
return TRUE;
return FALSE;
}
int main(int argc, char *argv[]){
printf("Assignment 2 Problem 1 by Jasmine Ramirez\n");
FILE * input = fopen("data_a2.txt", "r");
if(input == NULL){
printf("File %s not found.\n", "data_a2.txt");
return -1;
}
Stack *s;
s = create();
char str[255];
int i = 0, size = 1;
while(fscanf(input, "%[^\n]", str) == 1){
i++;
if(strcmp(str, "pop") == 0){
i--;
pop(s, &i);
//printf("#of elements after popping: %d\tstring popped: %s", i, temp);
}
else{
push(s, str, &size);
}
}
deleteStack(s);
fclose(input);
return 0;
}
這是輸入:(以防萬一)
to
sure
Be
pop
pop
pop
you
stop
won't
that
feeling
a
have
I
but
on
go
to
much
Not
right
Brilliant
happens
drink
pop
something
and
both
them
Ring
Story
ovaltine
your
pop
pop
Christmas
A
--
pop
pop
pop
pop
想法?或者我剛完全脫落?
爲什麼不使用''和'真',而是'FALSE'一做 - 類型的參數它自己'枚舉'? –
@below_avg_st函數empty和full必須在使用前聲明。這是錯誤的原因。 –
@JonathanLeffler我的教授希望我們使用枚舉。 –