栈的顺序存储结构的构建(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;
	}
}
相关推荐
蜡笔小马1 小时前
03.C++设计模式-原型模式
c++·设计模式·原型模式
神仙别闹1 小时前
基于QT(C++)实现线性表的建立、插入、删除、查找等基本操作
java·c++·qt
salipopl2 小时前
C/C++ 中 volatile 关键字详解:原理、作用与实际应用
开发语言·c++
张赫轩(不重名)2 小时前
图论3:连通性问题(复杂度均为 O(N + M) )
c++·算法·图论·拓扑学
AIminminHu2 小时前
(让 C++ 程序长出大脑:从“语音遥控器”到具身智能 Agent 的进化之路)------OpenGL渲染与几何内核那点事------(二-1-(15))
开发语言·c++·agent·具身智能
君义_noip2 小时前
CSP-J 2025 入门级 第一轮(初赛) 完善程序(1)
c++·算法·信息学奥赛·csp 第一轮
测绘第一深情3 小时前
在vscode中使用codex教程(个人安装经验)
数据结构·ide·vscode·python·算法·计算机视觉·编辑器
Liangwei Lin3 小时前
LeetCode 41. 缺失的第一个正数
数据结构·算法·leetcode
哭泣方源炼蛊3 小时前
AtCoder Beginner Contest 456 E补题(分层图 + 有向环检测 )
c++·算法·深度优先·图论·拓扑学
Yuk丶4 小时前
UE4 与 UE5:技术差异深度解析
c++·ue5·游戏引擎·ue4·游戏程序·虚幻