本文已参与「新人创作礼」活动,一起开启掘金创作之路。
一、两栈共享
如果有两个相同类型的栈,为它们各自开辟数组空间,极有可能是一个栈已经满了,而另一个栈可能还是空闲。这时我们就可以用一个数组来存储两个栈。将数组的两端作为两个栈底,top1,top2两个栈顶向中间靠拢,当top1+1==top2时,栈空间满。
typedef struct
{
ElementType data[MAXSIZE];
int top1;
int top2;
}DoubleStack;
二、两栈共享的相关操作实现
1.初始化
/*共享栈的初始化*/
void InitStack(DoubleStack *stack)
{
stack->top1=-1;
stack->top2=MAXSIZE;
}
2.清空共享栈
/*清空栈*/
void ClearStack(DoubleStack *stack)
{
stack->top1=-1;
stack->top2=MAXSIZE;
}
3.求栈空间中总的元素的个数
/*求栈中元素的个数*/
int StackLength(DoubleStack stack)
{
return (stack.top1+1)+(MAXSIZE-stack.top2);
}
3.指定栈并入栈
/*指定栈并入栈*/
int Push(DoubleStack *stack,ElementType e,int StackNumber)
{
if(stack->top1+1==stack->top2)
return 0;
if(StackNumber==1)
{
stack->data[++stack->top1]=e;
}else if(StackNumber==2)
{
stack->data[--stack->top2]=e;
}
return 1;
}
4.指定栈并出栈
/*指定栈并出栈*/
int Pop(DoubleStack *stack,ElementType *e,int StackNumber)
{
if(StackNumber==1)
{
if(stack->top1==-1)
return 0;
*e=stack->data[stack->top1--];
}
else if(StackNumber==2)
{
if(stack->top2==MAXSIZE)
return 0;
*e=stack->data[stack->top2++];
}
return 1;
}
5.依次遍历栈空间中的元素
/*依次遍历并输出栈空间中的元素*/
void StackTraverse(DoubleStack stack)
{
int i=0;
while(i<=stack.top1)
{
printf("%d ",stack.data[i++]);
}
i=stack.top2;
while(i<MAXSIZE)
{
printf("%d ",stack.data[i++]);
}
printf("\n");
}
三、具体代码实现
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef int ElementType;
typedef struct
{
ElementType data[MAXSIZE];
int top1;
int top2;
}DoubleStack;
/*共享栈的初始化*/
void InitStack(DoubleStack *stack)
{
stack->top1=-1;
stack->top2=MAXSIZE;
}
/*清空栈*/
void ClearStack(DoubleStack *stack)
{
stack->top1=-1;
stack->top2=MAXSIZE;
}
/*求栈中元素的个数*/
int StackLength(DoubleStack stack)
{
return (stack.top1+1)+(MAXSIZE-stack.top2);
}
/*指定栈并入栈*/
int Push(DoubleStack *stack,ElementType e,int StackNumber)
{
if(stack->top1+1==stack->top2)
return 0;
if(StackNumber==1)
{
stack->data[++stack->top1]=e;
}else if(StackNumber==2)
{
stack->data[--stack->top2]=e;
}
return 1;
}
/*指定栈并出栈*/
int Pop(DoubleStack *stack,ElementType *e,int StackNumber)
{
if(StackNumber==1)
{
if(stack->top1==-1)
return 0;
*e=stack->data[stack->top1--];
}
else if(StackNumber==2)
{
if(stack->top2==MAXSIZE)
return 0;
*e=stack->data[stack->top2++];
}
return 1;
}
/*依次遍历并输出栈空间中的元素*/
void StackTraverse(DoubleStack stack)
{
int i=0;
while(i<=stack.top1)
{
printf("%d ",stack.data[i++]);
}
i=stack.top2;
while(i<MAXSIZE)
{
printf("%d ",stack.data[i++]);
}
printf("\n");
}
/*样例测试*/
void StackTest()
{
DoubleStack stack;
int i;
ElementType e;
InitStack(&stack);
for(i=1;i<7;i++)
{
Push(&stack,i,1);
}
for(i=MAXSIZE;i>=MAXSIZE-2;i--)
{
Push(&stack,i,2);
}
printf("There are %d elements in the stack.\n",StackLength(stack));
printf("The elements in the stack are:\n");
StackTraverse(stack);
Pop(&stack,&e,2);
printf("The element popped out of the stack is %d.\n",e);
ClearStack(&stack);
printf("After emptying the stack, the number of elements in the stack is %d.\n",StackLength(stack));
}
int main()
{
StackTest();
return 0;
}
四、样例输出
There are 9 elements in the stack
The elements in the stack are: 1 2 3 4 5 6 98 99 100
The element popped out of the stack is 98.
After emptying the stack, the number of elements in the stack is 0.
五、写在最后
这里是数据结构个人学习的笔记记录,如有问题欢迎指正说明。