6-7 implementing two stacks in an array (20 points)
The subject requires two stacks to be implemented in an array.
function Interface Definition:
Stack CreateStack( int MaxSize );bool Push( Stack S, ElementType X, int Tag );ElementType Pop( Stack S, int Tag );
This Tag
is the stack number, which takes 1 or 2, MaxSize
the size of the stack array, and the Stack
structure definition as follows:
typedef int Position;struct SNode { ElementType *Data; Position Top1, Top2; int MaxSize;};typedef struct SNode *Stack;
Note: If the stack is full, the Push
function must output "stack fully" and return false; If a stack is empty, the Pop
function must output "stack tag empty" (where tag is the number of the stack) and return error.
Example of a referee test procedure:
#include <stdio.h> #include <stdlib.h> #define ERROR 1e8typedef int elementtype;typedef enum {push, pop, end} Operation;typedef Enum {false, true} bool;typedef int position;struct SNode {ElementType *data; Position Top1, TOP2; int MaxSize;}; typedef struct SNODE *stack; Stack createstack (int MaxSize); bool Push (Stack S, ElementType X, int Tag); ElementType Pop (Stack S, int Tag); Operation Getop (); /* Details omitted */void printstack (Stack S, int Tag); /* Details omitted */int main () {int N, Tag, X; Stack S; int done = 0; scanf ("%d", &n); S = Createstack (N); while (!done) {switch (Getop ()) {case push:scanf ("%d%d", &tag, &x); if (! Push (S, X, tag)) printf ("Stack%d is full!\n", tag); Break Case pop:scanf ("%d", &tag); X = Pop (S, Tag); if (x==error) printf ("Stack%d is empty!\n", Tag); Break Case END:Printstack (S, 1); Printstack (S, 2); Done = 1; Break }} return 0;} /* Your code will be embedded here */
Input Sample:
5Push 1 1Pop 2Push 2 11Push 1 2Push 2 12Pop 1Push 2 13Push 2 14Push 1 3Pop 2End
Sample output:
Stack 2 EmptyStack 2 is Empty!Stack FullStack 1 is Full!Pop from Stack 1: 1Pop from Stack 2: 13 12 11
思路:该指针是一维的,所以要用数组实现两个堆栈。我有两种想法申请一个2*MaxSize+1的数组,从中间开始分别加减实现两个栈,但是碰到输出超限,于是我打算补齐两个 /* details omitted */函数(细节决定成败,你却给我省略了)\脑疼??;然后试了第二种申请大小为MaxSize的数组,从两头开始向中。然而还是输出超限。。。。我要去百度了拜拜~
看完答案之后—>这种题目目的是为了让我们猜输出函数???简直浪费表情
两细节省略函数如下:
Operation Getop () {CharPush[] ="Push"; CharPop[] ="Pop"; CharEnd[] ="End"; Chars[ -]; scanf ("%s", s); if(strcmp (Push, s) = =0)returnpush; if(strcmp (Pop, s) = =0)returnPop; if(strcmp (End, s) = =0)returnend;}voidPrintstack (Stack S,intTag) {printf ("Pop from Stack%d:", Tag); if(Tag = =1){ while(S->top1! =-1) {printf ("%d", s->data[s->top1--]); } } Else { while(S->top2! = s->MaxSize) {printf ("%d", s->data[s->top2++]); }} putchar ('\ n');}
AC The code is as follows:
Stack Createstack (intMaxSize) {Stack stack= (Stack)malloc(sizeof(structSNode)); Stack->data = (int*)malloc(sizeof(ElementType) *MaxSize); Stack->TOP1 =-1; Stack->TOP2 =MaxSize; Stack->maxsize =MaxSize; returnStack;}BOOLPush (Stack S, ElementType X,intTag) { if(S = = NULL)return false; if(s->top1+1==s->Top2) {printf ("Stack full\n"); return false; } if(Tag = =1) S->DATA[++S->TOP1] =X; ElseS->DATA[--S->TOP2] =X; return true;} ElementType Pop (Stack S,intTag) { if(S = = NULL)returnERROR; if(Tag = =1){ if(S->top1 = =-1) {printf ("Stack%d empty\n", Tag); returnERROR; } returns->data[s->top1--]; } if(S->top2 = = s->MaxSize) {printf ("Stack%d empty\n", Tag); returnERROR; } returns->data[s->top2++]; }
6-7 implementing two stacks in an array