共享栈是两个栈在一个顺序的存储空间中。两个栈的栈底分别是存储空间的首尾地址。
代码如下:
stack.h
#pragma once
#include<stdio.h>
#define STACKMAXSIZE 1000
typedef char StackType;
/*定义一个共享栈的结构*/
typedef struct {
StackType data[STACKMAXSIZE];
/*top1和top2分别是两个栈的栈顶*/
int top1;
int top2;
}ShareStack;
/*初始化共享栈*/
void ShareStackInit(ShareStack* stack);
/*往栈里压入元素*/
void ShareStackPush(ShareStack* stack, StackType value, int StackNum);
/*出栈一个元素*/
void ShareStackPop(ShareStack* stack, int StackNum);
/*打印栈*/
void PrintStack(ShareStack* stack, int StackNum, char* msg);
/*取栈顶元素*/
StackType ShareStackGetTopValue(ShareStack* stack, int StackNUm);
/*销毁栈*/
void ShareStackDestory(ShareStack* stack, int StackNum);
stack.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"ShareStack.h"
/*初始化共享栈*/
void ShareStackInit(ShareStack* stack) {
if (stack == NULL) {
return;
}
stack->top1 = -1;
stack->top2 = STACKMAXSIZE;
}
/*往栈里压入元素*/
void ShareStackPush(ShareStack* stack, StackType value, int StackNum) {
if (stack == NULL) {
return;
}
/*判断栈满*/
if (stack->top1 + 1 == stack->top2) {
printf("栈满了");
return;
}
switch (StackNum) {
case 1:
/*往左边的栈压入元素*/
++stack->top1;
stack->data[stack->top1] = value;
break;
case 2:
--stack->top2;
stack->data[stack->top2] = value;
break;
default:
exit(0);
break;
}
}
/*出栈一个元素*/
void ShareStackPop(ShareStack* stack, int StackNum) {
if (stack == NULL) {
return;
}
switch (StackNum) {
case 1:
if (stack->top1 == -1) {
printf("栈1为空");
return;
}
--stack->top1;
break;
case 2:
if (stack->top2 == STACKMAXSIZE) {
printf("栈2为空");
return;
}
++stack->top2;
break;
default:
break;
}
}
/*打印栈*/
void PrintStack(ShareStack* stack, int StackNum, char* msg) {
if (stack == NULL) {
return;
}
switch (StackNum) {
case 1:
if (stack->top1 == -1) {
printf("栈为空\n");
return;
}
printf("%s\n", msg);
printf("[top]->");
int index = stack->top1;
while (index != -1) {
printf("[%c]->", stack->data[index]);
--index;
}
printf("[bottom]\n\n");
break;
case 2:
if (stack->top2 == STACKMAXSIZE) {
printf("栈为空\n");
return;
}
printf("%s\n", msg);
printf("[top]->");
int index2 = stack->top2;
while (index2 != STACKMAXSIZE) {
printf("[%c]->", stack->data[index2]);
++index2;
}
printf("[bottom]\n\n");
break;
default:
break;
}
}
/*取栈顶元素*/
StackType ShareStackGetTopValue(ShareStack* stack, int StackNUm) {
if (stack == NULL) {
return;
}
switch (StackNUm) {
case 1:
if (stack->top1 == -1) {
printf("栈1为空");
return;
}
return stack->data[stack->top1];
break;
case 2:
if (stack->top2 == STACKMAXSIZE) {
printf("栈2为空");
return;
}
return stack->data[stack->top2];
break;
default:
break;
}
}
/*销毁栈*/
void ShareStackDestory(ShareStack* stack, int StackNum) {
if (stack == NULL) {
return;
}
switch (StackNum) {
case 1:
stack->top1 = -1;
break;
case 2:
stack->top2 = STACKMAXSIZE;
break;
case 3:
stack->top1 = -1;
stack->top2 = STACKMAXSIZE;
break;
default:
break;
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include"ShareStack.h"
#define TESTHEAD printf("----------------%s-----------------\n",__FUNCTION__);
void TestPush() {
ShareStack stack;
TESTHEAD;
ShareStackInit(&stack);
/*开始测试push函数*/
ShareStackPush(&stack, 'a', 1);
ShareStackPush(&stack, 'b', 1);
ShareStackPush(&stack, 'c', 1);
ShareStackPush(&stack, 'd', 1);
ShareStackPush(&stack, 'e', 1);
ShareStackPush(&stack, 'e', 2);
ShareStackPush(&stack, 'd', 2);
ShareStackPush(&stack, 'c', 2);
ShareStackPush(&stack, 'b', 2);
ShareStackPush(&stack, 'a', 2);
PrintStack(&stack, 1, "压栈五个元素");
PrintStack(&stack, 2, "压栈五个元素");
}
void TestPop() {
ShareStack stack;
TESTHEAD;
ShareStackInit(&stack);
/*开始测试push函数*/
ShareStackPush(&stack, 'a', 1);
ShareStackPush(&stack, 'b', 1);
ShareStackPush(&stack, 'c', 1);
ShareStackPush(&stack, 'd', 1);
ShareStackPush(&stack, 'e', 1);
ShareStackPush(&stack, 'e', 2);
ShareStackPush(&stack, 'd', 2);
ShareStackPush(&stack, 'c', 2);
ShareStackPush(&stack, 'b', 2);
ShareStackPush(&stack, 'a', 2);
PrintStack(&stack, 1, "压栈五个元素");
PrintStack(&stack, 2, "压栈五个元素");
ShareStackPop(&stack, 1);
ShareStackPop(&stack, 1);
ShareStackPop(&stack, 2);
ShareStackPop(&stack, 2);
PrintStack(&stack, 1, "出栈两个元素");
PrintStack(&stack, 2, "出栈两个元素");
}
void TestGetTop() {
ShareStack stack;
TESTHEAD;
ShareStackInit(&stack);
/*开始测试push函数*/
ShareStackPush(&stack, 'a', 1);
ShareStackPush(&stack, 'b', 1);
ShareStackPush(&stack, 'c', 1);
ShareStackPush(&stack, 'd', 1);
ShareStackPush(&stack, 'e', 1);
ShareStackPush(&stack, 'e', 2);
ShareStackPush(&stack, 'd', 2);
ShareStackPush(&stack, 'c', 2);
ShareStackPush(&stack, 'b', 2);
ShareStackPush(&stack, 'a', 2);
PrintStack(&stack, 1, "压栈五个元素");
PrintStack(&stack, 2, "压栈五个元素");
StackType value1 = ShareStackGetTopValue(&stack, 1);
StackType value2 = ShareStackGetTopValue(&stack, 2);
printf("expect e, actual: %c\n", value1);
printf("expect a, actual: %c\n", value2);
}
void TestDestory() {
ShareStack stack;
TESTHEAD;
ShareStackInit(&stack);
/*开始测试push函数*/
ShareStackPush(&stack, 'a', 1);
ShareStackPush(&stack, 'b', 1);
ShareStackPush(&stack, 'c', 1);
ShareStackPush(&stack, 'd', 1);
ShareStackPush(&stack, 'e', 1);
ShareStackPush(&stack, 'e', 2);
ShareStackPush(&stack, 'd', 2);
ShareStackPush(&stack, 'c', 2);
ShareStackPush(&stack, 'b', 2);
ShareStackPush(&stack, 'a', 2);
PrintStack(&stack, 1, "压栈五个元素");
PrintStack(&stack, 2, "压栈五个元素");
ShareStackDestory(&stack, 1);
PrintStack(&stack, 1, "销毁栈1");
ShareStackDestory(&stack, 2);
PrintStack(&stack, 2, "销毁栈1");
}
int main() {
TestPush();
TestPop();
TestGetTop();
TestDestory();
system("pause");
return;
}