一个数组实现两个栈(共享栈)

思路:一个数组实现两个栈,简单的来想就是要把一个数组分成两部分

方式一:

 

 下标为0的位置是stack1的栈底,下标为1的位置是stack2的栈底,stack1的元素放在下标为偶数的位置,stack2的元素放在下标为奇数的位置。这种分法有一个缺点就是容易造成空间浪费,假如stack1只有2个元素,而stack2有10个元素,最后使用的总空间还是20,但其实只存了12个元素。

方式二:

从中间向两边生长的方式

方式三:

两边向中间生长的方式

下面代码实现的是方式三

//TStack.h
#pragma once


//一个数组实现两个栈

//两边往中间生长的方式
typedef struct TStack {
	int  *array;
	int	 top1;
	int  top2;
	int capacity;
}TStack;

void TStackInit(TStack* pTS);        //初始化
void TStackDestroy(TStack* pTS);     //销毁
void ExpanfIfRequired(TStack* pTS);  //扩容
void Push_1(TStack* pTS, int data);  //向栈1添加数据
void Push_2(TStack* pTS, int data);  //向栈2添加数据
void Pop_1(TStack* pTS);             //栈1出栈
void Pop_2(TStack* pTS);             //栈2出栈
int Top_1(TStack* pTS);              //获取栈1的栈顶元素
int Top_2(TStack* pTS);              //获取栈2的栈顶元素
int Size_1(TStack* pTS);             //获取栈1元素个数
int Size_2(TStack* pTS);             //获取栈2元素个数

void TestTStack();                   //测试函数
//TStack.c
#include "TStack.h"
#include <assert.h>
#include "Stack.h"
#include <stdio.h>
void TStackInit(TStack* pTS)
{
	if (pTS == NULL) {
		return;
	}
	pTS->capacity = 10;
	pTS->array = (int*)malloc(sizeof(int)*pTS->capacity);
	pTS->top1 = 0;
	pTS->top2 = pTS->capacity - 1;
}
void TStackDestroy(TStack* pTS)
{
	assert(pTS);
	pTS->capacity = 0;
	pTS->top1 = 0;
	pTS->top2 = 0;
	free(pTS->array);
	pTS->array = NULL;
}
void ExpanfIfRequired(TStack* pTS)
{
	//断言
	assert(pTS);
	//栈1指针没有超过栈2指针,不需要扩容
	if (pTS->top1 <= pTS->top2) {
		return;
	}
	//扩容
	int newCapacity = (pTS->capacity) * 2;
	int* newArray = (int*)malloc(sizeof(int)*newCapacity);
	//判断新数组空间是否开辟成功
	assert(newArray);
	//进行数据搬移
	for (int i = 0; i < pTS->top1; i++) {
		newArray[i] = pTS->array[i];
	}
	//栈2进行搬移 [top1, capcacity),原数组
	for (int j = pTS->top1; j < pTS->capacity; j++) {
		int k = j + pTS->capacity;
		newArray[k] = pTS->array[j];
	}
	//更新数组
	pTS->top2 += pTS->capacity;
	pTS->capacity = newCapacity;

	//销毁原数组
	free(pTS->array);
	pTS->array = newArray;
}
void Push_1(TStack* pTS, int data)
{
	ExpanfIfRequired(pTS);
	pTS->array[pTS->top1] = data;
	pTS->top1++;
}
void Push_2(TStack* pTS, int data)
{
	ExpanfIfRequired(pTS);
	pTS->array[pTS->top2] = data;
	pTS->top2--;
}
void Pop_1(TStack* pTS)
{
	pTS->top1--;
}
void Pop_2(TStack* pTS)
{
	pTS->top2++;
}
int Top_1(TStack* pTS)
{
	return pTS->array[pTS->top1 - 1];
}
int Top_2(TStack* pTS)
{
	return pTS->array[pTS->top2 + 1];
}
int Size_1(TStack* pTS)
{
	return pTS->top1;
}
int Size_2(TStack* pTS)
{
	return pTS->capacity - (pTS->top2) - 1;
}

//测试函数
void TestTStack()
{
	TStack stack;
	TStackInit(&stack);

	Push_1(&stack, 1);
	Push_1(&stack, 2);
	Push_1(&stack, 3);
	Push_1(&stack, 4);
	Push_1(&stack, 5);
	Push_1(&stack, 6);
	Push_1(&stack, 7);

	Push_2(&stack, 101);
	Push_2(&stack, 102);
	Push_2(&stack, 103);
	for (int i = 0; i < (&stack)->capacity; i++) {
		printf("%d ",(&stack)->array[i]);
	}
	printf("\n");
	// 触发扩容
	Push_2(&stack, 104);
	Push_2(&stack, 105);

	for (int i = 0; i < (&stack)->capacity; i++) {
		printf("%d ", (&stack)->array[i]);
	}
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值