另类堆栈
来源:互联网 发布:如何在阿里云备案域名 编辑:程序博客网 时间:2024/05/16 01:32
习题3.14 另类堆栈 (15分)
在栈的顺序存储实现中,另有一种方法是将Top定义为栈顶的上一个位置。请编写程序实现这种定义下堆栈的入栈、出栈操作。如何判断堆栈为空或者满?
函数接口定义:
bool Push( Stack S, ElementType X );ElementType Pop( Stack S );
其中Stack
结构定义如下:
typedef int Position;typedef struct SNode *PtrToSNode;struct SNode { ElementType *Data; /* 存储元素的数组 */ Position Top; /* 栈顶指针 */ int MaxSize; /* 堆栈最大容量 */};typedef PtrToSNode Stack;
注意:如果堆栈已满,Push
函数必须输出“Stack Full”并且返回false;如果队列是空的,则Pop
函数必须输出“Stack Empty”,并且返回ERROR。
裁判测试程序样例:
#include <stdio.h>#include <stdlib.h>#define ERROR -1typedef int ElementType;typedef enum { push, pop, end } Operation;typedef enum { false, true } bool;typedef int Position;typedef struct SNode *PtrToSNode;struct SNode { ElementType *Data; /* 存储元素的数组 */ Position Top; /* 栈顶指针 */ int MaxSize; /* 堆栈最大容量 */};typedef PtrToSNode Stack;Stack CreateStack( int MaxSize ){ Stack S = (Stack)malloc(sizeof(struct SNode)); S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType)); S->Top = 0; S->MaxSize = MaxSize; return S;}bool Push( Stack S, ElementType X );ElementType Pop( Stack S );Operation GetOp(); /* 裁判实现,细节不表 */void PrintStack( Stack S ); /* 裁判实现,细节不表 */int main(){ ElementType X; Stack S; int N, done = 0; scanf("%d", &N); S = CreateStack(N); while ( !done ) { switch( GetOp() ) { case push: scanf("%d", &X); Push(S, X); break; case pop: X = Pop(S); if ( X!=ERROR ) printf("%d is out\n", X); break; case end: PrintStack(S); done = 1; break; } } return 0;}/* 你的代码将被嵌在这里 */
输入样例:
4PopPush 5Push 4Push 3PopPopPush 2Push 1Push 0Push 10End
输出样例:
Stack Empty3 is out4 is outStack Full0 1 2 5
#include <stdio.h>#include <stdlib.h>#include <string.h>#define ERROR -1typedef int ElementType;typedef enum {push, pop, end} Operation;typedef enum {false, true} bool;typedef int Position;typedef struct SNode *PtrToSNode;struct SNode{ElementType *Data;Position Top;int MaxSize;};typedef PtrToSNode Stack;Stack CreateStack(int MaxSize){Stack S = (Stack)malloc(sizeof(struct SNode));S->Data = (ElementType *)malloc(MaxSize*sizeof(ElementType));S->Top=0;S->MaxSize = MaxSize;return S;}bool Push(Stack S, ElementType X);ElementType Pop(Stack S);Operation GetOp();void PrintStack(Stack S);int main(){ElementType X;Stack S;int N, done=0;scanf("%d", &N);S = CreateStack(N);while(!done){switch(GetOp()){case push:scanf("%d", &X);Push(S, X);break;case pop:X = Pop(S);if(X!=ERROR)printf("%d is out\n", X);break;case end:PrintStack(S);done = 1;break;}}return 0;}Operation GetOp(){ char a[5]; scanf("%s",a); if(strcmp(a,"Push")==0) return push; else if(strcmp(a,"Pop")==0) return pop; else if(strcmp(a,"End")==0) return end; else return -1; } bool Push(Stack S, ElementType X){if(S->Top-1==S->MaxSize-1){printf("Stack Full\n");return false;}S->Data[S->Top]=X;S->Top++;return true;}ElementType Pop(Stack S){ElementType X;if(S->Top==0){printf("Stack Empty\n");return ERROR;}S->Top--;X = S->Data[S->Top];return X;}void PrintStack(Stack S){int i,n;n = S->Top-1;for(i=n;i>=0;i--)printf("%d ", S->Data[i]);printf("\n");}
阅读全文
0 0
- 另类堆栈
- 找游戏Call之另类方法:查看堆栈法
- PTA_6-4 另类堆栈(15 分)_单指针栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 堆栈
- 等待(wait)和通知(notify)
- Leetcode Insert Delete GetRandom O(1) Duplicates allowed and Not allowed
- Android--使用VideoView播放视频
- FragmentPagerAdapter与FragmentStatePagerAdapter区别
- JS判断用户是PC端还是移动端进入
- 另类堆栈
- java web中简单调用摄像头和话筒
- SpringMVC工作流程及其核心类
- 杂谈
- 人脸技术概述
- 深入解析Android Design包——NestedScroll
- [雅礼集训6-23] T1 电报
- 遗传算法模拟解决TSP问题
- x-editable教程