那個運用c++編寫兩個程序,其中一個是實現二叉樹(或者棧),另一個是運用二叉樹(或者棧)解決實際問題。謝謝啦,實在是沒搞懂。
/**
typedef struct TStack {
Element *base;
Element *top;
int size;
} *Stack;
//棧的構造器,創建空棧
void stackConstructor(Stack &stack){
stack->base = (Element *)malloc(INIT_SIZE * sizeof(Element));
if(!stack->base){
printf("\n為棧分配內存空間失敗!\n");
exit(0);
}
stack->top = stack->base; //空棧 top == base
stack->size = INIT_SIZE;
}
//是否為空棧
bool isEmpty(Stack stack){
if(stack->top == stack->base){
return true;
}
return false;
}
//壓棧
bool push(Stack &stack, Element e){
if(stack->top - stack->base >= stack->size){ //棧滿
stack->base = (Element *)realloc(stack->base, (stack->size + INCREMENT_SIZE) * sizeof(Element));
if(!stack->base){
printf("\n為棧擴展內存空間失敗!\n");
return false;
}
stack->top = stack->base + stack->size;
stack->size += INCREMENT_SIZE;
}
*stack->top++ = e;
return true;
}
//彈棧
Element pop(Stack stack){
if(isEmpty(stack)){
printf("\n棧為空,彈棧操作失敗!\n");
return ' ';
}
return *--stack->top;
}
/**
int main() {
Stack stack;
stackConstructor(stack);
push(stack, 'f');
push(stack, 'a');
push(stack, 'n');
push(stack, 'c');
push(stack, 'y');
printf("%c", pop(stack));
printf("%c", pop(stack));
printf("%c", pop(stack));
printf("%c", pop(stack));
printf("%c", pop(stack));
//output[result]: ycnaf
return 0;
}