第4章:2.两栈共享空间

本篇文章参考的是《大话数据结构》,感谢作者程杰先生。

一:
两栈共享同一空间,即数组,数组有两个端点,两个栈有两个栈底,
让一个栈的栈底为数组的时段,即下标为0处,另一个栈的栈底为数组的末端,
即数组长度n-1处,这样,两个栈如果增加元素,就是两个端点向中间延伸。
在这里插入图片描述
二:如何判断栈满?
1.普通情况下,当top1与top2见面时,为栈满,
即 top1 + 1 = top2;

2.极端情况,若栈2是空栈,栈1的top1等于n-1,就是栈1满了;
当栈1为空时,top2等于0时,为栈2满。

三:
代码:

/*
 
本篇文章参考的是《大话数据结构》,感谢作者程杰先生。

*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#include "bigtalk_data_structure.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;


static Status visit(SElemType c)
{
    
    
	printf("%d ",c);
	return OK;
}

/*  构造一个空栈S */
static Status InitStack(SqDoubleStack *S)
{
    
     
	S->top1=-1;
	S->top2=MAXSIZE;
	return OK;
}

/* 把S置为空栈 */
static Status ClearStack(SqDoubleStack *S)
{
    
     
	S->top1=-1;
	S->top2=MAXSIZE;
	return OK;
}

/* 若栈S为空栈,则返回TRUE,否则返回FALSE */
static Status StackEmpty(SqDoubleStack S)
{
    
     
	if (S.top1==-1 && S.top2==MAXSIZE)
		return TRUE;
	else
		return FALSE;
}

/* 返回S的元素个数,即栈的长度 */
static int StackLength(SqDoubleStack S)
{
    
     
	return (S.top1+1)+(MAXSIZE-S.top2);
}

/* 插入元素e为新的栈顶元素 */
static Status Push(SqDoubleStack *S,SElemType e,int stackNumber)
{
    
    
	if (S->top1+1==S->top2)	/* 栈已满,不能再push新元素了 */
		return ERROR;	
	
	if (stackNumber==1)			/* 栈1有元素进栈 */
		S->data[++S->top1]=e; /* 若是栈1则先top1+1后给数组元素赋值。 */
	else if (stackNumber==2)	/* 栈2有元素进栈 */
		S->data[--S->top2]=e; /* 若是栈2则先top2-1后给数组元素赋值。 */

	return OK;
}

/* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */
static Status Pop(SqDoubleStack *S,SElemType *e,int stackNumber)
{
    
     
	if (stackNumber==1) 
	{
    
    
		if (S->top1==-1) 
			return ERROR; /* 说明栈1已经是空栈,溢出 */
		*e=S->data[S->top1--]; /* 将栈1的栈顶元素出栈 */
	}
	else if (stackNumber==2)
	{
    
     
		if (S->top2==MAXSIZE) 
			return ERROR; /* 说明栈2已经是空栈,溢出 */
		*e=S->data[S->top2++]; /* 将栈2的栈顶元素出栈 */
	}
	return OK;
}

static 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;
}

//栈:两栈共享空间
void test_main_4_5()
{
    
    
	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	

	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));

	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	

	Pop(&s,&e,2);
	printf("弹出的栈顶元素 e=%d\n",e);
	printf("栈空否:%d(1:空 0:否)\n",StackEmpty(s));

	printf("栈中元素依次为:");
	StackTraverse(s);
	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	


	for(j = 6;j <= MAXSIZE-2; j++)
		Push(&s,j,1);
	
	printf("栈中元素依次为:");
	StackTraverse(s);
	
	printf("栈满否:%d(1:否 0:满)\n",Push(&s,100,1));
	

	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	

	ClearStack(&s);
	printf("清空栈后,栈空否:%d(1:空 0:否)\n",StackEmpty(s));
	
	#if 0

	#endif

    return 0;
}




打印:

[main:14]:[yang] ***************************************** 
[test_main_4_5:125]:[yang] ******************* 我是分割线******************* 
栈中元素依次为:1 2 3 4 5 18 19 20 
当前栈中元素有:8 
[test_main_4_5:144]:[yang] ******************* 我是分割线******************* 
弹出的栈顶元素 e=18
栈空否:0(1:0:)
栈中元素依次为:1 2 3 4 5 19 20 
[test_main_4_5:152]:[yang] ******************* 我是分割线******************* 
栈中元素依次为:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
栈满否:0(1:0:)
[test_main_4_5:164]:[yang] ******************* 我是分割线******************* 
清空栈后,栈空否:1(1:0:)

猜你喜欢

转载自blog.csdn.net/yanghangwww/article/details/110732641