基础介绍
共享栈就是一块内存分配给两个栈,两个栈从各自端点向中间生长,就是计算机基础中内存栈的实现吧
代码
#include "stdio.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef int Status;
typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */
//两栈共享空间结构
typedef struct
{
SElemType data[MAXSIZE];
int top1; /* 栈1栈顶指针 */
int top2; /* 栈2栈顶指针 */
}SqDoubleStack;
Status visit(SElemType c)
{
printf("%d ", c);
return OK;
}
Status InitStack(SqDoubleStack* S)
{
S->top1 = -1;
S->top2 = MAXSIZE;
return OK;
}
// 把S置为空栈
const Status ClearStack(SqDoubleStack* S)
{
S->top1 = -1;
S->top2 = MAXSIZE;
return OK;
}
//判断是否为空栈
Status StackEmpty(SqDoubleStack* S)
{
if (S->top1 == -1 && S->top2 == MAXSIZE)
return TRUE;
else
{
return FALSE;
}
}
//返回元素的个数
int StackLength(SqDoubleStack* S)
{
return (S->top1 + 1) + (MAXSIZE - S->top2);
}
//插入元素e为某个栈的新元素
Status Push(SqDoubleStack* S, SElemType e, int stackNumber)
{
if (S->top1 + 1 == S->top2)
return ERROR;
else if (stackNumber == 1)
{
S->top1++;
S->data[S->top1] = e;
}
else
{
S->top2--;
S->data[S->top2] = e;
}
return OK;
}
//返回栈顶元素
Status Pop(SqDoubleStack* S, SElemType* e, int stackNumber)
{
if (stackNumber == 1)
{
if (S->top1 == -1)
return ERROR;
*e = S->data[S->top1--];
}
else if (stackNumber == 2)
{
if (S->top2 == MAXSIZE)
return ERROR;
*e = S->data[S->top2++];
}
return OK;
}
const Status StackTraverse(SqDoubleStack* S)
{
int i;
i = 0;
while (i <= S->top1)
{
visit(S->data[i++]);
}
i = S->top2;
while (i < MAXSIZE)
{
visit(S->data[i++]);
}
printf("\n");
return OK;
}
int main()
{
int j;
SqDoubleStack s;
int e;
if (InitStack(&s) == OK)
{
for (j = 1; j <= 5; j++)
Push(&s, j, 1);
for (j = MAXSIZE; j >= MAXSIZE - 2; j--)
Push(&s, j, 2);
}
printf("栈中元素依次为:");
StackTraverse(&s);
printf("当前栈中元素有:%d \n", StackLength(&s));
Pop(&s, &e, 2);
printf("弹出的栈顶元素 e=%d\n", e);
printf("栈空否:%d(1:空 0:否)\n", StackEmpty(&s));
for (j = 6; j <= MAXSIZE - 2; j++)
Push(&s, j, 1);
printf("栈中元素依次为:");
StackTraverse(&s);
printf("栈满否:%d(1:否 0:满)\n", Push(&s, 100, 1));
ClearStack(&s);
printf("清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(&s));
return 0;
}