#include<stdio.h>
#include<stdlib.h>
#define STACK_INT_SIZE 10
#define STACKINCREMENT 5
#define OK 1
#define ERROR 0
#define MAXQSIZE 51
typedef int ElemType;
typedef int QElemType; /*队列元素类型*/
//栈的基本操作
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
} SqStack;
//初始化栈
int InitStack(SqStack *s)
{
s->base=(ElemType *)malloc(STACK_INT_SIZE*sizeof(ElemType));
if(!s->base)
return ERROR;
s->top=s->base;
s->stacksize=STACK_INT_SIZE;
return OK;
}
//判断栈空
int EmptyStack(SqStack *s)
{
if(s->top==s->base)
return OK;
else
return ERROR;
}
//入栈
int PushStack(SqStack *s, ElemType e)
{
if(s->top-s->base==s->stacksize)
{
s->base=(ElemType *)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!s->base)
return ERROR;
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top=e;
s->top++;
return OK;
}
//出栈
int PopStack(SqStack *s,ElemType *e)
{
if(s->base==s->top)
return ERROR;
s->top--;
*e=*s->top;
return OK;
}
//取栈顶元素
int GetTop(SqStack *s,ElemType *e)
{
if(s->base==s->top)
return ERROR;
*e=*(s->top-1);
return OK;
}
//队列的基本操作
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
/*初始化队列*/
int InitQueue(SqQueue *Q)
{
Q->base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
if(!Q->base)
return ERROR;
Q->front=Q->rear=0;
return OK;
}/*InitQueue*/
/*求队长*/
int QueueLength(SqQueue *Q)
{
if(Q->rear>Q->front)
return Q->rear-Q->front;
else
return Q->rear+MAXQSIZE-Q->front;
}/*QueueLentgh*/
/*入队*/
int EnQueue(SqQueue *Q,QElemType e)
{
if((Q->rear+1)%MAXQSIZE==Q->front)
return ERROR;
Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%MAXQSIZE;
return OK;
}/*EnQuese*/
/*出队*/
int DeQueue(SqQueue *Q,QElemType *e)
{
if(Q->front==Q->rear)
return ERROR;
*e=Q->base[Q->front];
Q->front=(Q->front+1)%MAXQSIZE;
return OK;
}/*DeQueue*/
/*判队空*/
int QueueEmpty(SqQueue *Q)
{
if(Q->front==Q->rear)
return OK;
else
return ERROR;
}/*QueueEmpty*/
/*取队头*/
int GetHead(SqQueue *Q,QElemType *e)
{
if(Q->front==Q->rear)
return ERROR;
*e=Q->base[Q->front];
return OK;
}/*GetHead*/
/*销毁队列*/
int DestroyQueue(SqQueue *Q)
{
if(Q->base)
{
Q->rear=Q->front=0;
free(Q->base);
}
return OK;
}/*DestroyQueue*/
标签:return,int,mytool,玩具,源文件,base,front,top,rear
From: https://blog.51cto.com/u_16030624/6193484