top函数的作用C语言,C数据结构, Status GetTop(SqStack S,SElemType &e)函数,S.top-1与S.top-为啥S.top-不行...

本文详细解析了C语言中顺序栈的基本操作实现,包括初始化、压栈、弹栈及获取栈顶元素等函数的编写,并探讨了S.top-1与S.top--在获取栈顶元素函数中的区别。
部署运行你感兴趣的模型镜像

当前位置:我的异常网» C语言 » C数据结构, Status GetTop(SqStack S,SElemType &a

C数据结构, Status GetTop(SqStack S,SElemType &e)函数,S.top-1与S.top-为啥S.top-不行

www.myexceptions.net  网友分享于:2013-11-13  浏览:42次

C数据结构, Status GetTop(SqStack S,SElemType &e)函数,S.top-1与S.top--?为什么S.top--不行?

是不是e=*(S.top--)运算顺序问题?刚才试了一下,e=*(--S.top)就可以了

#include

#include

#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=(int *)malloc(STACK_INIT_SIZE*sizeof(struct SqStack));

S.top=S.base;

S.stacksize=0;

return OK;

// 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE

// 请补全代码

}

Status Push(SqStack &S,SElemType e)

{

int *newbase;

if(S.stacksize>=STACK_INIT_SIZE)

{

newbase=(int *)realloc(S.base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(struct SqStack));

S.base=newbase;

}

S.base[S.stacksize]=e;

S.stacksize++;

S.top++;

return OK;

// 在栈S中插入元素e为新的栈顶元素

// 请补全代码

}

Status Pop(SqStack &S,SElemType &e)

{

if(S.top==S.base)

return ERROR;

else

{

e=*(S.top-1);///////////////////////////////////e=S.base[S.stacksize];????????????????????????????????????//

S.top--;

S.stacksize--;////////////////////////////////////free()caozuobudong

}

// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR

// 请补全代码

}

Status GetTop(SqStack S,SElemType &e)

{

if(S.top==S.base)

return ERROR;

else

{

e=*(S.top-1);//////////////S.top--???????????????????????????????????????????????????????

}

return OK;

// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR

// 请补全代码

}

int StackLength(SqStack S)

{

return S.stacksize;

// 返回栈S的元素个数

// 请补全代码

}

Status StackTraverse(SqStack S)

{

// 从栈顶到栈底依次输出栈中的每个元素

SElemType *p=(SElemType *)malloc(sizeof(SElemType));

p=S.top; //请填空

if(p==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)) // 判断顺序表是否创建成功,请填空

{

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;

}

}

}

------解决方案--------------------

文章评论

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

/***顺序栈的实现***/ #include<iostream> using namespace std; #include<stdlib.h> //顺序栈定义 #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 100 typedef int Status; typedef int SElemType; typedef struct{ SElemType *base; SElemType *top; int stacksize; }SqStack; //算法3.1 顺序栈的初始化 Status InitStack(SqStack &S) {// 构造一个空栈 S S.base = new SElemType[MAXSIZE]; //为顺序栈分配一个最大容量为MAXSIZE的数组空间 if(!S.base) exit (OVERFLOW); //存储分配失败 S.top = S.base; S.stacksize = MAXSIZE; return OK; } //算法3.2 顺序栈的入栈 Status Push(SqStack &S,SElemType &e) { // 插入元素e为新的栈顶元素 if(S.top-S.base==S.stacksize) return ERROR; //栈满 *(S.top++) = e; //元素e压入栈顶,栈顶指针加1 return OK; } //算法3.3 顺序栈的出栈 Status Pop(SqStack &S,SElemType &e) {// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR if(S.base == S.top) return ERROR;//栈空 e = *(--S.top); //栈顶指针减1,将栈顶元素赋给e return OK; } //算法3.4 取顺序栈的栈顶元素 Status GetTop(SqStack S,SElemType &e) {// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR if(S.top == S.base) return ERROR; e = *(S.top-1);//栈顶指针减1,将栈顶元素赋给e return OK; } int main() { SqStack s; SElemType e; SElemType t; if(InitStack(s)==OK) for(int j=1;j<=12;j++) { Push(s,j); } while(GetTop(s,e) == OK) { Pop(s,t); } return 0; } 要求: (1)利用栈实现中缀表达式求值 目的: 深化栈的综合应用能力 掌握表达式求值的核心算法逻辑 提升问题解决能力用c语言表示
最新发布
11-16
#include <stdio.h> #include <stdlib.h> // 宏定义状态码栈参数 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define STACK_INIT_SIZE 100 // 初始栈容量 #define STACKINCREMENT 10 // 栈满时扩容增量 // 类型定义 typedef int Status; typedef int SElemType; // 顺序栈存储结构 typedef struct { SElemType *base; // 栈底指针 SElemType *top; // 栈顶指针 int stacksize; // 当前栈最大容量 } SqStack; // 1. 初始化顺序栈 Status InitStack(SqStack &S) { S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if (!S.base) exit(OVERFLOW); // 内存分配失败 S.top = S.base; // 栈顶 = 栈底,空栈状态 S.stacksize = STACK_INIT_SIZE; return OK; } // 2. 入栈(插入元素) Status Push(SqStack &S, SElemType e) { if (S.top - S.base >= S.stacksize) { // 栈满,扩容 S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType)); if (!S.base) exit(OVERFLOW); S.top = S.base + S.stacksize; // 更新栈顶指针 S.stacksize += STACKINCREMENT; // 更新栈容量 } *S.top++ = e; // 元素入栈,栈顶指针上移 return OK; } // 3. 出栈(删除栈顶元素) Status Pop(SqStack &S, SElemType &e) { if (S.top == S.base) return ERROR; // 栈空,出栈失败 e = *--S.top; // 栈顶指针下移,取出原栈顶元素 return OK; } // 4. 取栈顶元素(不删除) Status GetTop(SqStack S, SElemType &e) { if (S.top == S.base) return ERROR; // 栈空,无栈顶元素 e = *(S.top - 1); // 直接读取栈顶元素(不移动指针) return OK; } // 5. 遍历顺序栈(从栈底到栈顶) Status TraverseStack(SqStack &S) { if (S.top == S.base) { printf("栈为空!\n"); return OK; } SElemType *p = S.base; int i = 1; printf("顺序栈元素(栈底→栈顶):"); while (p < S.top) { printf("第%d个元素:%d ", i++, *p++); } printf("\n"); return OK; } // 6. 置空顺序栈 Status SetEmpty(SqStack &S) { S.top = S.base; // 栈顶指针指向栈底,栈空(不释放内存) return OK; } // 7. 销毁顺序栈(释放内存) Status DestroyStack(SqStack &S) { if (S.base) free(S.base); S.base = S.top = NULL; S.stacksize = 0; return OK; } // 主函数(测试所有基本运算) int main() { SqStack S; Status status; SElemType e; int choice; // 初始化栈 InitStack(S); printf("顺序栈初始化完成!\n"); // 菜单驱动测试 while (1) { printf("\n===== 顺序栈基本运算菜单 =====\n"); printf("1. 入栈 2. 出栈 3. 取栈顶元素 4. 遍历栈 5. 置空栈 6. 退出\n"); printf("请选择操作(1-6):"); scanf("%d", &choice); switch (choice) { case 1: printf("请输入入栈元素(整数):"); scanf("%d", &e); status = Push(S, e); status == OK ? printf("元素%d入栈成功!\n", e) : printf("入栈失败!\n"); break; case 2: status = Pop(S, e); status == OK ? printf("出栈元素:%d\n", e) : printf("栈空,出栈失败!\n"); break; case 3: status = GetTop(S, e); status == OK ? printf("栈顶元素:%d\n", e) : printf("栈空,无栈顶元素!\n"); break; case 4: TraverseStack(S); break; case 5: SetEmpty(S); printf("顺序栈已置空!\n"); break; case 6: DestroyStack(S); printf("程序退出,栈已销毁!\n"); return 0; default: printf("输入错误,请重新选择!\n"); } } } 改写以上代码,将错误代码修改正确,使其能够在visual studio 2022中运行
11-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值