Chapter 4: 2. Two-stack shared space

This article refers to "Dahua Data Structure", thanks to the author Mr. Cheng Jie.

One: The
two stacks share the same space, that is, the array. The array has two endpoints, and the two stacks have two stack bottoms.
Let the bottom of one stack be the period of the array, that is, the subscript is 0 and the bottom of the other stack. It is the end of
the array, that is, at the length of the array n-1, so that if two stacks add elements, the two ends extend to the middle.
Insert picture description here
2: How to judge the stack is full?
1. Under normal circumstances, when top1 and top2 meet, the stack is full,
that is, top1 + 1 = top2;

2. In extreme cases, if stack 2 is empty and top1 of stack 1 is equal to n-1, stack 1 is full;
when stack 1 is empty and top2 is equal to 0, stack 2 is full.

Three:
Code:

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

*/

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




print:

[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:)

Guess you like

Origin blog.csdn.net/yanghangwww/article/details/110732641