``
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "malloc.h"
#include "stdlib.h"
#define STACK_TNTI_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define ERROR 0
#define FALSE 0
#define OK 1
#define INFEASIBLE -1
#define OVERFLOW -2
typedef char SElemType;
typedef int Status;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status StackEmpty(SqStack S);
Status initstack(SqStack *S);
Status Push(SqStack *S ,SElemType e);
Status Pop(SqStack *S, SElemType *e);
Status StackEmpty(SqStack S){
if (S.top = S.base)
return TRUE;
}
Status initstack(SqStack *S){
S->base = (SElemType *)malloc(STACK_TNTI_SIZE * sizeof(SElemType));
if(!S->base) exit (OVERFLOW);
S->top = S->base;
S->stacksize = STACK_TNTI_SIZE;
return OK;
}
Status Push(SqStack *S, SElemType e){
if(S->top - S->base >= S->stacksize){
S->base = (SElemType *)realloc(S->base,(S->stacksize + STACK_TNTI_SIZE)*sizeof(SElemType));
if (!S->base) exit(OVERFLOW);
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMENT;
}
*(S->top++) = e; /* *(S->top) = e; S->top++; */
return OK;
}
Status Pop(SqStack *S, SElemType *e)
{
if(S->top == S->base) return ERROR;
*e = *(--S->top);
return OK;
}
#define MAXQSIZE 100
typedef char QElemType;
typedef struct{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue (SqQueue *Q);
int QueueLength (SqQueue Q);
Status EnQueue (SqQueue *Q, QElemType e);
Status DeQueue (SqQueue *Q, QElemType *e);
Status InitQueue (SqQueue *Q){
Q->base = (QElemType * ) malloc (MAXQSIZE * sizeof (QElemType));
if(!Q->base) exit (OVERFLOW);
Q->front = Q->rear = 0;
return OK;
}
int QueueLength (SqQueue Q){
return(Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
Status EnQueue (SqQueue *Q, QElemType e){
if ((Q->rear + 1) % MAXQSIZE == Q->front) return ERROR;
/* *((*Q).base + Q->rear) = e;*/
Q->base[Q->rear] = e;
Q->rear = (Q->rear + 1) % MAXQSIZE;
return OK;
}
Status DeQueue (SqQueue *Q, QElemType *e){
if (Q->front == Q->rear)
return ERROR;
*e = Q->base[Q->front];
/* e = *((*Q).base + (*Q).front); */
Q->front = (Q->front + 1) % MAXQSIZE;
return OK;
}
int main(){
SqQueue Q;
SqStack S;
SElemType e;
QElemType m;
InitQueue (&Q);
initstack(&S);