栈的顺序存储结构的构建(C++)+ 两栈共享空间

栈顶指针为-1的时候代表栈为空

栈的类定义 :

cpp 复制代码
const int N = 1000;
class SStack{
	public:
		SStack();//构造空栈
		~SStack();//析构函数
		void push(int x);//入栈
		int pop();//出栈
		int getTop();//取栈顶元素
		bool empty();//判空
    private:
    	int data[N];//存放栈元素的数组
    	int top;//栈顶指针
};
cpp 复制代码
#include <iostream>
using namespace std;
const int N = 1000;
class SStack{
	public:
		SStack();//构造空栈
		~SStack();//析构函数
		void push(int x);//入栈
		int pop();//出栈
		int getTop();//取栈顶元素
		bool empty();//判空
    private:
    	int data[N];//存放栈元素的数组
    	int top;//栈顶指针
};
SStack :: SStack()//构造空栈
{
	top = -1;
}
void SStack :: push(int x)//入栈
{
	if(top == N - 1)  throw "上溢";
	data[++top] = x;
}
int SStack :: pop()//出栈
{
	if(top == -1)  throw "下溢";
	int x = data[top--];
	return x;
}
int SStack :: getTop()//取栈顶元素
{
	if(top == -1)  throw "下溢";
	int x = data[top];
	return x;
}
bool SStack :: empty()//判空
{
	if(top == -1)  return true;
	else  return false;
}

两栈共享空间:两栈存储的数据类型相同,两栈的空间变化相反,一个变长另一个就要变短。

当top1 = -1那么栈1为空,当top2 = N,那么栈2为空

类定义:

cpp 复制代码
const int N = 1000;
class Double_SStack{
	public:
		Double_SStack();//构造空栈
		~Double_SStack();//析构函数
		void push(int x,int SStack_num);//入栈
		int pop(int SStack_num);//出栈
		int getTop(int SStack_num);//取栈顶元素
		bool empty(int SStack_num);//判空
    private:
    	int data[N];//存放栈元素的数组
    	int top1;//栈顶指针1
    	int top2;//栈顶指针2
};
cpp 复制代码
#include <iostream>
using namespace std;
const int N = 1000;
class Double_SStack{
	public:
		Double_SStack();//构造空栈
		~Double_SStack();//析构函数
		void push(int x,int SStack_num);//入栈
		int pop(int SStack_num);//出栈
		int getTop(int SStack_num);//取栈顶元素
		bool empty(int SStack_num);//判空
    private:
    	int data[N];//存放栈元素的数组
    	int top1;//栈顶指针
    	int top2;
};
Double_SStack :: Double_SStack()//构造空栈
{
	top1 = -1;
	top2 = N;
}
void Double_SStack :: push(int x , int SStack_num)//入栈
{
	if(top1 + 1 == top2)  throw "上溢";
	if(SStack_num == 1)  data[++top1] = x;
	else if(SStack_num == 2)  data[--top2] = x;
}
int Double_SStack :: pop(int SStack_num)//出栈
{
	int x;//存数
	if(SStack_num == 1)
	{
		if(top1 == -1)  throw "下溢";
		x = data[top1--];
	}
	else if(SStack_num == 2)
	{
		if(top2 == N)  throw "下溢";
		x = data[top2++];
	}
	return x;
}
int Double_SStack :: getTop(int SStack_num)//取栈顶元素
{
	int x;
	if(SStack_num == 1)
	{
		if(top1 == -1)  throw "下溢";
		x = data[top1];
	}
	else if(SStack_num == 2)
	{
		if(top2 == N)  throw "下溢";
		x = data[top2];
	}
	return x;
}
bool Double_SStack :: empty(int SStack_num)//判空
{
	if(SStack_num == 1)
	{
		if(top1 == -1)  
		    return true;
		else
		    return false;
	}
	else if(SStack_num == 2)
	{
		if(top2 == N)
		    return true;
		else 
		    return false;
	}
}
相关推荐
greentea_20135 分钟前
Codeforces Round 173 B. Digits(2043)
c++·算法
Q741_1471 小时前
C++ 位运算 高频面试考点 力扣 面试题 17.19. 消失的两个数字 题解 每日一题
c++·算法·leetcode·面试·位运算
初圣魔门首席弟子2 小时前
C++ STL string(字符串)学习笔记
c++·笔记·学习
AA陈超2 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P04-12 可缩放浮点数的曲线表
c++·游戏·ue5·游戏引擎·虚幻
旭意2 小时前
C++微基础备战蓝桥杯之数组篇10.1
开发语言·c++·蓝桥杯
LGL6030A2 小时前
数据结构学习(2)——多功能链表的实现(C语言)
数据结构·学习·链表
nsjqj2 小时前
数据结构:栈和队列
数据结构
xwl12123 小时前
10.6 作业
数据结构·算法
青草地溪水旁3 小时前
VSCode C/C++ 构建任务配置文件 `tasks.json` 全字段深度解析
c语言·c++·vscode
爱和冰阔落5 小时前
C++模板进阶 非类型模板参数 模板的特化 分离编译的深入探索
c++·面试·编译原理·模板