ShareStack.h
#pragma once
/*
顺序表的两端分别作为双栈的栈底,当整个线性表没有空间时,双栈满
*/
#define MAX_SIZE 20
#define ERROR -1
#define OVERFLOW -2
#define LEFT 0
#define RIGHT 1
typedef char ElemType;
typedef struct {
ElemType* base;
ElemType* l_top;
ElemType* r_top;
}s_stack;
void InitStack(s_stack& S);//初始化栈
void DestroyStack(s_stack& S);//销毁栈
bool EmptyStack(s_stack S,int stack_num);//判断栈是否为空
void ClearStack(s_stack& S);//清空栈
int SizeStack(s_stack S,int stack_num);//栈内元素数量
void Push(s_stack& S, int stack_num, ElemType e);//入栈
void Pop(s_stack& S, int stack_num, ElemType& e);//出栈
void TraverseStack(s_stack S,int stack_num,void(*f)(ElemType e));//遍历栈
ShareStack.c
#include <stdlib.h>
#include <stdio.h>
#include "ShareStack.h"
void InitStack(s_stack& S) {
S.base =(ElemType*) malloc(MAX_SIZE * sizeof(ElemType));
if (!S.base) exit(ERROR);
S.l_top = S.base;
S.r_top = S.base + MAX_SIZE - 1;
}//初始化栈
void DestroyStack(s_stack& S) {
free(S.base);
S.l_top = S.r_top = NULL;
}//销毁栈
bool EmptyStack(s_stack S, int stack_num) {
if (stack_num == LEFT) return S.base == S.l_top ? true : false;
else if (stack_num == RIGHT) return S.base == S.r_top - MAX_SIZE + 1 ? true : false;
else exit(ERROR);
}//判断栈是否为空
void ClearStack(s_stack& S) {
S.l_top = S.base;
S.r_top = S.base + MAX_SIZE - 1;
}//清空栈
int SizeStack(s_stack S, int stack_num) {
if (stack_num == LEFT) return S.l_top - S.base;
else if (stack_num == RIGHT) return S.base + MAX_SIZE - 1 - S.r_top;
else exit(ERROR);
}//栈内元素数量
void Push(s_stack& S, int stack_num, ElemType e) {
if (S.l_top > S.r_top)//判断共享栈是不是已满
exit(OVERFLOW);
if (stack_num == LEFT) {
*(S.l_top++)=e;
}
else if (stack_num == RIGHT) {
*(S.r_top--) = e;
}
else {
exit(ERROR);
}
}//入栈
void Pop(s_stack& S, int stack_num, ElemType& e) {
if (EmptyStack(S, stack_num)) exit(ERROR);
if (stack_num == LEFT) {
e = *(--S.l_top);
}
else if (stack_num == RIGHT) {
e = *(++S.r_top);
}
else {
exit(ERROR);
}
}//出栈
void TraverseStack(s_stack S, int stack_num, void(*f)(ElemType e)) {
ElemType* p;
if (stack_num == LEFT) {
printf("stack 0:\n");
p = S.l_top;
while(p-- != S.base) {
f(*(p));
}
}
else if (stack_num == RIGHT) {
printf("stack 1:\n");
p = S.r_top;
while (p++!=S.base+MAX_SIZE-1)
{
f(*(p));
}
}
else {
exit(ERROR);
}
}//遍历栈
main.c(用于测试)
#include <stdlib.h>
#include <stdio.h>
#include "ShareStack.h"
void InitStack(s_stack& S) {
S.base =(ElemType*) malloc(MAX_SIZE * sizeof(ElemType));
if (!S.base) exit(ERROR);
S.l_top = S.base;
S.r_top = S.base + MAX_SIZE - 1;
}//初始化栈
void DestroyStack(s_stack& S) {
free(S.base);
S.l_top = S.r_top = NULL;
}//销毁栈
bool EmptyStack(s_stack S, int stack_num) {
if (stack_num == LEFT) return S.base == S.l_top ? true : false;
else if (stack_num == RIGHT) return S.base == S.r_top - MAX_SIZE + 1 ? true : false;
else exit(ERROR);
}//判断栈是否为空
void ClearStack(s_stack& S) {
S.l_top = S.base;
S.r_top = S.base + MAX_SIZE - 1;
}//清空栈
int SizeStack(s_stack S, int stack_num) {
if (stack_num == LEFT) return S.l_top - S.base;
else if (stack_num == RIGHT) return S.base + MAX_SIZE - 1 - S.r_top;
else exit(ERROR);
}//栈内元素数量
void Push(s_stack& S, int stack_num, ElemType e) {
if (S.l_top > S.r_top)//判断共享栈是不是已满
exit(OVERFLOW);
if (stack_num == LEFT) {
*(S.l_top++)=e;
}
else if (stack_num == RIGHT) {
*(S.r_top--) = e;
}
else {
exit(ERROR);
}
}//入栈
void Pop(s_stack& S, int stack_num, ElemType& e) {
if (EmptyStack(S, stack_num)) exit(ERROR);
if (stack_num == LEFT) {
e = *(--S.l_top);
}
else if (stack_num == RIGHT) {
e = *(++S.r_top);
}
else {
exit(ERROR);
}
}//出栈
void TraverseStack(s_stack S, int stack_num, void(*f)(ElemType e)) {
ElemType* p;
if (stack_num == LEFT) {
printf("stack 0:\n");
p = S.l_top;
while(p-- != S.base) {
f(*(p));
}
}
else if (stack_num == RIGHT) {
printf("stack 1:\n");
p = S.r_top;
while (p++!=S.base+MAX_SIZE-1)
{
f(*(p));
}
}
else {
exit(ERROR);
}
}//遍历栈