当前位置:网站首页>Basic operations of 8583 sequential stack
Basic operations of 8583 sequential stack
2022-08-02 14:18:00 【weixin_50862344】
题干
(go see for yourself)
代码实现
#include<malloc.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量
typedef int SElemType; // 定义栈元素类型
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{
// 请补全代码
S.base= (SElemType*)malloc(STACK_INIT_SIZE *sizeof(SElemType*));
if(!S.base)
return ERROR;//Forgot to add this paragraph at first
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
// 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE
return OK;//Forgot to add this paragraph at first,通过不了
}
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=STACK_INIT_SIZE)//扩展
{
S.base= (SElemType*)malloc((STACK_INIT_SIZE+STACKINCREMENT) *sizeof(SElemType*));
if(!S.base) return ERROR;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top=e;
S.top++;
// 在栈S中插入元素e为新的栈顶元素
// 请补全代码
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.base==S.top) return ERROR;
e=*(S.top-1);//TOPAlways be next to the top element on the stack
S.top--;
return OK;
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
// 请补全代码
}
Status GetTop(SqStack S,SElemType &e)
{
if(S.base==S.top) return ERROR;
e=*(S.top-1);
return OK;
// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
// 请补全代码
}
int StackLength(SqStack S)
{
if(S.base==S.top)return 0;
int n;
n=S.top-S.base;
return n;
//return (int)(S.top-S.base);
// 返回栈S的元素个数
// 请补全代码
}
Status StackTraverse(SqStack S)
{
// 从栈顶到栈底依次输出栈中的每个元素
SElemType *p = (SElemType *)malloc(sizeof(SElemType));
p = S.top; //请填空
if(S.top==S.base)printf("The Stack is Empty!"); //请填空
else
{
printf("The Stack is: ");
p--;//小细节
while(p>=S.base) //请填空
{
//请填空
printf("%d ", *p);
p--;
}
}
printf("\n");
return OK;
}
int main()
{
int a;
SqStack S;
SElemType x, e;
if(InitStack(S)) // Determine whether the sequence table is created successfully,请填空
{
printf("A Stack Has Created.\n");
}
while(1)
{
printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");
scanf("%d",&a);
switch(a)
{
case 1: scanf("%d", &x);
if(!Push(S,x)) printf("Push Error!\n"); // 判断Push是否合法,请填空
else printf("The Element %d is Successfully Pushed!\n", x);
break;
case 2: if(!Pop(S,e)) printf("Pop Error!\n"); // 判断Pop是否合法,请填空
else printf("The Element %d is Successfully Poped!\n", e);
break;
case 3: if(!GetTop(S,e))printf("Get Top Error!\n"); // 判断Get Top是否合法,请填空
else printf("The Top Element is %d!\n", e);
break;
case 4: printf("The Length of the Stack is %d!\n",StackLength(S)); //请填空
break;
case 5:StackTraverse(S); //请填空
break;
case 0: return 1;
}
}
}
小细节
- Operates on the base address each time(包括扩展,创建…),After the operation is completed, it is necessary to judge whether it is successful or not
(Although it doesn't seem to matteroj结果
if(!S.base) return ERROR;
- 进栈push中
*S.top=e;
top是栈顶指针,可以理解为:*Manipulate it to look liken一样的变量
- Status StackTraverse(SqStack S)函数中
SElemType *p = ______________________ //请填空
其实只要写 *SElemType p;
oj就可以通过
边栏推荐
猜你喜欢
Break the limit of file locks and use storage power to help enterprises grow new momentum
Sentinel源码(六)ParamFlowSlot热点参数限流
Flask框架的搭建及入门
rhce第三天作业
深度学习框架pytorch快速开发与实战chapter4
动态刷新日志级别
第十一单元 序列化器
MobileNet ShuffleNet & yolov5替换backbone
Supervision strikes again, what about the market outlook?2021-05-22
Data Organization---Chapter 6 Diagram---Graph Traversal---Multiple Choice Questions
随机推荐
浅浅写一下PPOCRLabel的使用及体验
第五单元 保持状态
Minio文件上传
第八单元 中间件
replay视频播放器_怎么让手机音乐跟视频一起放
You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
【Tensorflow】AttributeError: module ‘keras.backend‘ has no attribute ‘tf‘
redis分布式锁和看门狗的实现
MySQL数据库语法格式
网络安全第一次作业(2)
微信小程序-最近动态滚动实现
paddleocr window10初体验
第七单元 ORM表关系及操作
第十五单元 分页、过滤
无序数组排序并得到最大间隔
【ROS】工控机的软件包不编译
rhce第三天作业
MySQL数据库设计规范
mysql的case when如何用
RowBounds[通俗易懂]