那个运用c++编写两个程序,其中一个是实现二叉树(或者栈),另一个是运用二叉树(或者栈)解决实际问题。谢谢啦,实在是没搞懂。
2条回答
- threenewbee 2014-12-11 11:26关注
/**
- <!--
- File : stack.h
- Author : fancy
- Email : fancydeepin@yeah.net
- Date : 2013-02-03
- --!> */ #include #include #include #define Element char #define INIT_SIZE 10 #define INCREMENT_SIZE INIT_SIZE / 2
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;
}/**
- <!--
- File : Stack.cpp
- Author : fancy
- Email : fancydeepin@yeah.net
- Date : 2013-02-03
- --!> */ #include "stack.h"
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;
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#python#的问题:求帮写python代码
- ¥20 MATLAB画图图形出现上下震荡的线条
- ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
- ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
- ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
- ¥15 perl MISA分析p3_in脚本出错
- ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
- ¥15 ubuntu虚拟机打包apk错误
- ¥199 rust编程架构设计的方案 有偿
- ¥15 回答4f系统的像差计算