#ifndef ZYDL_H
#define ZYDL_H
#include"Head.h"
#define STACK_INT_SIZE 100
#define STACKINCREMENT 10
#define DuQueue_MAX_SIZE 10
#define MAXQSIZE 100
typedef int SElemType;
typedef int QElemType;
//顺序栈
typedef struct {
SElemType* base, * top;//栈底,栈顶指针
int stacksize;//已分配的存储空间
}Stack;
//单链队列
typedef struct QNode{
QElemType data;
struct QNode* next;
}QNode,*QueuePtr;
typedef struct {
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;
//循环队列
//顺序存储
typedef struct {
QElemType* base;
int front;
int rear;
}SqQueue;
//初始化
Status InitStack(Stack* S);
Status InitLinkQueue(LinkQueue* Q);
Status InitSqQueue(SqQueue* Q);
//销毁
Status Destory_Stack(Stack* S);
Status Destory_LinkQueue(LinkQueue* Q);
Status Destory_SqQueue(SqQueue* Q);
//置空
Status Clear_Stack(Stack* S);
Status Clear_LinkQueue(LinkQueue* Q);
Status Clear_SqQueue(SqQueue* Q);
//探空
Status Empty_Stack(Stack S);
Status Empty_LinkQueue(LinkQueue Q);
Status Empty_SqQueue(SqQueue Q);
//求栈长
int Length_Stack(Stack S);
int Length_LinkQueue(LinkQueue Q);
int Length_SqQueue(SqQueue Q);
//获取栈顶元素
Status GetTop(Stack S, SElemType* e);
//获取队头元素
Status GetHead(LinkQueue Q, QElemType* e);
//判断循环队列是否满
Status FullSqQueue(SqQueue Q);
//e入栈
Status Push(Stack* S, SElemType e);
//入队列
Status EnQueue(LinkQueue* Q, QElemType e);
Status EnSqQueue(SqQueue* Q, QElemType e);
//出栈并返回e
Status Pop(Stack* S, SElemType* e);
//出队列并返回e
Status DeQueue(LinkQueue* Q, QElemType* e);
Status DeSqQueue(SqQueue* Q, QElemType* e);
#endif // !ZYDL_H
#ifndef ZYDL_C
#define ZYDL_C
#include"Stack_Queue.h"
//初始化
Status InitStack(Stack* S) {
(*S).base = MALLOC(STACK_INT_SIZE, SElemType);
(*S).top = (*S).base;
(*S).stacksize = STACK_INT_SIZE;
return OK;
}
Status InitLinkQueue(LinkQueue* Q) {
(*Q).front = MALLOC(1, QNode);
(*Q).rear = (*Q).front;
(*Q).front->next = NULL;
return OK;
}
Status InitSqQueue(SqQueue* Q) {
(*Q).base = MALLOC(DuQueue_MAX_SIZE, QElemType);
(*Q).front = (*Q).rear = 0;
return OK;
}
//销毁
Status Destory_Stack(Stack* S) {
free((*S).base);
(*S).base = NULL;
(*S).top = (*S).base;
(*S).stacksize = 0;
return OK;
}
Status Destory_LinkQueue(LinkQueue* Q) {
while ((*Q).front) {
(*Q).rear = (*Q).front->next;
free((*Q).front);
(*Q).front = (*Q).rear;
}
return OK;
}
Status Destory_SqQueue(SqQueue* Q) {
free((*Q).base);
(*Q).front = 0;
(*Q).rear = 0;
return OK;
}
//置空
Status Clear_Stack(Stack* S) {
(*S).stacksize = 0;
(*S).top = (*S).base;
return OK;
}
Status Clear_LinkQueue(LinkQueue* Q) {
(*Q).rear = (*Q).front;
(*Q).front->next = NULL;
return OK;
}
Status Clear_SqQueue(SqQueue* Q) {
(*Q).rear = (*Q).front;
return OK;
}
//探空
Status Empty_Stack(Stack S) {
return S.base==S.top ? OK : ERROR;
}
Status Empty_LinkQueue(LinkQueue Q) {
return Q.front == Q.rear ? OK : ERROR;
}
Status Empty_SqQueue(SqQueue Q) {
return Q.front == Q.rear ? OK : ERROR;
}
//求栈长
int Length_Stack(Stack S) {
return S.top - S.base;
}
int Length_LinkQueue(LinkQueue Q) {
QueuePtr p = Q.front;
int len = 0;
while (p != Q.rear) {
len++;
p = p->next;
}
return len;
}
int Length_SqQueue(SqQueue Q) {
return (Q.rear - Q.front + DuQueue_MAX_SIZE) % DuQueue_MAX_SIZE;
}
//获取栈顶元素
Status GetTop(Stack S, SElemType* e) {
if (!S.stacksize)
return ERROR;
*e = *(S.top - 1);
return OK;
}
//获取队头元素
Status GetHead(LinkQueue Q, QElemType* e) {
if (Q.front == Q.rear)
return ERROR;
*e = Q.front->data;
return OK;
}
//判断循环队列是否满
Status FullSqQueue(SqQueue Q) {
return (Q.rear + 1) % DuQueue_MAX_SIZE == Q.front ? OK : ERROR;
}
//入栈
Status Push(Stack* S, SElemType e) {
if ((*S).top - (*S).base >= STACK_INT_SIZE)
(*S).base = REALLOC((*S).base, ((*S).stacksize + STACKINCREMENT), SElemType);
*((*S).top++) = e;
return OK;
}
//入队列
Status EnQueue(LinkQueue* Q, QElemType e) {
QueuePtr p = MALLOC(1, QNode);
p->data = e;
p->next = NULL;
(*Q).rear->next = p;
(*Q).rear = p;
return OK;
}
Status EnSqQueue(SqQueue* Q, QElemType e) {
if (FullSqQueue(*Q))
return ERROR;
(*Q).base[(*Q).rear] = e;
(*Q).rear = ((*Q).rear + 1) % DuQueue_MAX_SIZE;
return OK;
}
//出栈并用e返回
Status Pop(Stack* S, SElemType* e) {
if ((*S).base == (*S).top)
return ERROR;
*e = *(--(*S).top);
return OK;
}
//出队列并用e返回
Status DeQueue(LinkQueue* Q, QElemType* e) {
if ((*Q).front == (*Q).rear)
return OK;
*e = (*Q).front->next->data;
QueuePtr p = (*Q).front;
(*Q).front = (*Q).front->next;
free(p);
return OK;
}
Status DeSqQueue(SqQueue* Q, QElemType* e) {
if (Empty_SqQueue(*Q))return ERROR;
*e = (*Q).base[(*Q).front];
(*Q).front = ((*Q).front + 1) % DuQueue_MAX_SIZE;
return OK;
}
#endif // !ZYDL_C