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 1e8
typedef int ElementType;
typedef enum {
push, pop, end } Operation;
typedef struct StackRecord *<