没有合适的资源?快使用搜索试试~ 我知道了~
数据结构
资源推荐
资源详情
资源评论
题目 1:用动态和静态 2 种内存分配方式分别编程实现顺序栈的创建、入栈、出栈、判断栈
空和栈满操作?
答:动态
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20
//#define gets(str) gets_s(str) //解决 vs 不能用 gets
#define scanf scanf_s
typedef char ElemType;
typedef int Status;
//结构类型的描述
typedef struct stack {
ElemType *data; //元素数组
int top; //栈顶指针 并不是 C 的指针
} SqStack;
//顺序栈的初始化
void initStack(SqStack &st) //初始化栈
{ st.top = -1; }
//判断是否为空
Status stackEmpty(SqStack st) //判断钱是否为空 当然可以用 bool 类型
{
if (st.top == -1)
return TRUE;
else
return FALSE;
}
Status stackman(SqStack st) //判断钱是否为空 当然可以用 bool 类型
{
if (st.top ==MAXSIZE)
return TRUE;
else
return FALSE;
}
//进栈
Status push(SqStack &st, ElemType x) //进栈 *
{ if(st.top==MAXSIZE) return FALSE;
st.top++;
st.data[st.top]=x;
return TRUE;
}
//出栈
Status pop(SqStack &st, ElemType &x)//出栈
{ if(st.top==-1) return FALSE;
x=st.data[st.top];
st.top--;
return TRUE;
}
//得到栈顶元素
Status getTop(SqStack &st, ElemType &x)
{ if(st.top==-1) return FALSE;
x=st.data[st.top];
return TRUE;
}
//打印栈中的数据
void printStack(SqStack &st)
{
printf("顺序栈显示:");
for (int i = 0; i <= st.top; i++)
printf("%d-", st.data[i]);
printf("\n");
}
//测试
int main()
{
SqStack st;
st.data = (ElemType*)malloc(sizeof(ElemType)*20); //申请 MAXSIZE 个元素内存
ElemType x; //用来保存出栈带回来的值
printf("进行初始化\n");
initStack(st);
printf("判断是否栈空\n");
if (stackEmpty(st) == OK)
printf("栈空\n");
else
printf("非空\n");
printf("判断是否栈满\n");
if (stackman(st) == OK)
printf("栈满\n");
else
printf("非满\n");
printf("入栈测试\n");
for (int i = 0; i < 10; i++)
剩余18页未读,继续阅读
资源评论
菜包咕咕嘎嘎
- 粉丝: 0
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功