4-2-Two Stacks In One Array
来源:互联网 发布:gossip协议《算法》 编辑:程序博客网 时间:2024/06/06 03:23
4-2 Two Stacks In One Array
Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used.
Format of functions:
Stack CreateStack( int MaxElements );int IsEmpty( Stack S, int Stacknum );int IsFull( Stack S );int Push( ElementType X, Stack S, int Stacknum );ElementType Top_Pop( Stack S, int Stacknum );
where int Stacknum
is the index of a stack which is either 1 or 2; int MaxElements
is the size of the stack array; and Stack
is defined as the following:
typedef struct StackRecord *Stack;struct StackRecord { int Capacity; /* maximum size of the stack array */ int Top1; /* top pointer for Stack 1 */ int Top2; /* top pointer for Stack 2 */ ElementType *Array; /* space for the two stacks */}
Note: Push
is supposed to return 1 if the operation can be done successfully, or 0 if fails. If the stack is empty, Top_Pop
must return ERROR
which is defined by the judge program.
Sample program of judge:
#include <stdio.h>#include <stdlib.h>#define ERROR 1e8typedef int ElementType;typedef enum { push, pop, end } Operation;typedef struct StackRecord *Stack;struct StackRecord { int Capacity; /* maximum size of the stack array */ int Top1; /* top pointer for Stack 1 */ int Top2; /* top pointer for Stack 2 */ ElementType *Array; /* space for the two stacks */};Stack CreateStack( int MaxElements );int IsEmpty( Stack S, int Stacknum );int IsFull( Stack S );int Push( ElementType X, Stack S, int Stacknum );ElementType Top_Pop( Stack S, int Stacknum );Operation GetOp(); /* details omitted */void PrintStack( Stack S, int Stacknum ); /* details omitted */int main(){ int N, Sn, X; Stack S; int done = 0; scanf("%d", &N); S = CreateStack(N); while ( !done ) { switch( GetOp() ) { case push: scanf("%d %d", &Sn, &X); if (!Push(X, S, Sn)) printf("Stack %d is Full!\n", Sn); break; case pop: scanf("%d", &Sn); X = Top_Pop(S, Sn); if ( X==ERROR ) printf("Stack %d is Empty!\n", Sn); break; case end: PrintStack(S, 1); PrintStack(S, 2); done = 1; break; } } return 0;}/* Your function will be put here */
Sample Input:
5Push 1 1Pop 2Push 2 11Push 1 2Push 2 12Pop 1Push 2 13Push 2 14Push 1 3Pop 2End
Sample Output:
Stack 2 is Empty!Stack 1 is Full!Pop from Stack 1: 1Pop from Stack 2: 13 12 11
Stack CreateStack( int MaxElements ){Stack p;p = (struct StackRecord *)malloc(sizeof(struct StackRecord));p->Capacity = MaxElements;p->Top1 = -1;p->Top2 = MaxElements;p->Array = (ElementType *)malloc(MaxElements*sizeof(ElementType));return p;}int IsEmpty( Stack S, int Stacknum ){if(Stacknum == 1)return S->Top1 == -1;elsereturn S->Top2 == S->Capacity;}int IsFull( Stack S ){return S->Top1 == S->Top2 - 1;}int Push( ElementType X, Stack S, int Stacknum ){if(IsFull(S))return 0;if(Stacknum == 1)S->Array[++S->Top1] = X;elseS->Array[--S->Top2] = X;return 1;}int Top_Pop(Stack S, int Stacknum ){if(IsEmpty(S, Stacknum))return ERROR;if(Stacknum == 1)return S->Array[S->Top1--];elsereturn S->Array[S->Top2++];}
1 0
- 4-2-Two Stacks In One Array
- PTA 4-2 Two Stacks In One Array
- Two Stacks In One Array
- 6-1 Two Stacks In One Array-PTA双指指针stack表操作实现
- 6-18 Two Stacks In One Array(20 point(s))
- pta Two Stacks In One Array(简单版双向数组)
- Two stack in one array
- CC150 3.1 Three in one: describe how you could use a single array to implement 3 stacks.
- Timus 1495. One-two, One-two 2
- URAL 1495. One-two, One-two 2
- URAL 1495. One-two, One-two 2
- [DS][3-21]Implement two stacking using only one array
- URAL 1495. One-two, One-two 2 DP or BFS?
- URAL 1495. One-two, One-two 2(暴力枚举)
- 【构造】 URAL 1495 One-two, One-two 2
- URAL 1495 One-two, One-two 2 (BFS)
- 算法练习:URAL 1495 One-two, One-two 2
- How to capture the one-to-one dependency between two attributes in one relation?
- android IntentService分析
- 1209日常小结
- 按键70秒,Root轻松得:Linux惊现高危漏洞
- socket编程之bind()函数
- java 文件、文件夹 递归复制、移动
- 4-2-Two Stacks In One Array
- R语言-数据预处理的一些实用(万能)办法:缺失值、数据重复、共线性等等的处理
- 函数,函数指针
- acm蛇形填数
- Sorry, Ubuntu xx.xx Has Experienced An Internal Error ubuntu14.04更新不了问题解决
- Linux 五个最牛视频编辑软件
- 正则表达式
- Java NIO详解(一): FileChannel
- C++类 学校,班级,学生的三级关系