当前位置:网站首页>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就可以通过
边栏推荐
猜你喜欢
Diodes and their applications
如何解决mysql服务无法启动1069
跑跑yolov5吧
政策利空对行情没有长期影响,牛市仍将继续 2021-05-19
如何解决1045无法登录mysql服务器
Differences and concepts between software testing and hardware testing
网络安全第五次作业
第二讲 软件生命周期
Configure zabbix auto-discovery and auto-registration.
Interview | with questions to learn, Apache DolphinScheduler Wang Fuzheng
随机推荐
Flask框架深入一
Sentinel源码(四)(滑动窗口流量统计)
rpm包的卸载与安装[通俗易懂]
如何自定义feign方法级别的超时时间
drf路由组件Routers
AWVS工具介绍[通俗易懂]
Raft对比ZAB协议
网络安全第二次作业
RHCE第一天作业
深度学习框架pytorch快速开发与实战chapter4
How to solve mysql service cannot start 1069
The bad policy has no long-term impact on the market, and the bull market will continue 2021-05-19
Data Organization---Chapter 6 Diagram---Graph Traversal---Multiple Choice Questions
期货具体是如何开户的?
WeChat Mini Program-Recent Dynamic Scrolling Implementation
第七单元 ORM表关系及操作
不精确微分/不完全微分(Inexact differential/Imperfect differential)
Flask框架的搭建及入门
Shell脚本完成pxe装机配置
chapter6可视化(不想看版)