双栈共享存储空间(C++代码实现)

理论部分

理论来源:b站up主:跟懒猫老师快乐数据结构

image-20210523104641877

2021-05-23_095948

2021-05-23_100025

2021-05-23_100149

2021-05-23_100249

2021-05-23_100345

C++代码

#include<iostream>
using namespace std;
const int STACKSIZE = 6;
//两栈共享存储空间编程
//使用类模板编程
template<class DataType>
class BothStack
{
private:
	DataType *data;//属性,顺序栈,数组
	int top1, top2;//属性,top1指向栈1的栈顶,top2指向栈2的栈顶
	int capacity;//属性,栈的容量
public:
	BothStack();//无参构造函数
	BothStack(int size);//有参构造函数
	~BothStack();//析构函数
	DataType getTop(int num);//弹出栈顶元素,num表明对栈1操作,还是对栈2操作
	DataType Pop(int num);//出栈
	void Push(int num, DataType elem);//入栈
	bool isFull();//判断栈满
	bool isEmpty(int num);//判断栈空
	class IndexError{};//定义内部异常类
	class Empty{};
	class Full{};
};

template<class DataType>
BothStack<DataType>::BothStack()
{
	data = new DataType[STACKSIZE];
	top1 = -1;
	top2 = STACKSIZE;
	capacity = STACKSIZE;
}



template<class DataType>
BothStack<DataType>::BothStack(int size)
{
	data = new DataType[size];
	top1 = -1;
	top2 = size;
	capacity = size;
}



template<class DataType>
BothStack<DataType>::~BothStack()
{
	delete[] data;
}



template<class DataType>
DataType BothStack<DataType>::getTop(int num)
{
	if (num == 1)
	{
		//对栈1进行操作
		if (isEmpty(num))
		{
			throw Empty();
		}
		else
		{
			return data[top1];
		}
	}
	else if (num == 2)
	{
		//对栈2进行操作
		if (isEmpty(num))
		{
			throw Empty();
		}
		else
		{
			return data[top2];
		}
	}
	else
	{
		throw IndexError();
	}
}

template<class DataType>
DataType BothStack<DataType>::Pop(int num)
{
	if (num == 1)
	{
		//对栈1进行操作
		if (isEmpty(num))
		{
			throw Empty();
		}
		else
		{
			return data[top1--];
		}
	}
	else if (num == 2)
	{
		//对栈2进行操作
		if (isEmpty(num))
		{
			throw Empty();
		}
		else
		{
			return data[top2++];
		}
	}
	else
	{
		throw IndexError();
	}
}



template<class DataType>
void BothStack<DataType>::Push(int num, DataType elem)
{
	if (isFull())
	{
		throw Full();
	}
	else {
		if (num == 1)
		{
			data[++top1] = elem;
		}
		else if (num == 2)
		{
			data[--top2] = elem;
		}
		else {
			throw IndexError();
		}
	}
}
template<class DataType>
bool BothStack<DataType>::isFull()
{
	if (top2 == top1 +1)
	{
		return true;
	}
	return false;
}
template<class DataType>
bool BothStack<DataType>::isEmpty(int num)
{
	if (num == 1)
	{
		if (top1 == -1)
		{
			return true;
		}
		return false;
	}
	else if (num == 2)
	{
		if (top2 == capacity)
		{
			return true;
		}
		return false;
	}
	else
	{
		throw IndexError();
	}
}
int main()
{
	BothStack<char> bs1;
	//栈1中依次压入'a','b','c'
	//栈2中依次压入'z','x','y'
	//栈3中压入'z',抛出异常
	//try--catch捕获异常对象
	try {
		bs1.Push(1, 'a');
		bs1.Push(1, 'b');
		bs1.Push(1, 'c');
		bs1.Push(2, 'z');
		bs1.Push(2, 'y');
		bs1.Push(2, 'x');
		bs1.Push(2, 'w');
	}
	catch (BothStack<char>::Full)
	{
		cout << "Stack Full!!!" << endl;
	}
	catch (BothStack<char>::IndexError)
	{
		cout << "Index Error!!!" << endl;
	}


	try {
		cout << bs1.Pop(1) << endl;
		cout << bs1.Pop(1) << endl;
		cout << bs1.Pop(1) << endl;
		
		cout << bs1.Pop(2) << endl;
		cout << bs1.Pop(2) << endl;
		cout<<bs1.Pop(2)<<endl;
		cout << bs1.Pop(2) << endl;
		
	}
	catch (BothStack<char>::Empty)
	{
		cout << "Stack Empty!!!"<< endl;
	}
	catch (BothStack<char>::IndexError)
	{
		cout << "Index Error!!!" << endl;
	}
}

运行结果

栈的大小为6

image-20210523111250488

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以,以下是双栈结构的C语言实现: ``` #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 typedef struct { int data[MAX_SIZE]; int top1; // 1顶指针 int top2; // 2顶指针 } DoubleStack; // 初始化双栈 void initDoubleStack(DoubleStack *s) { s->top1 = -1; s->top2 = MAX_SIZE; } // 判断1是否为空 int isStack1Empty(DoubleStack *s) { return s->top1 == -1; } // 判断2是否为空 int isStack2Empty(DoubleStack *s) { return s->top2 == MAX_SIZE; } // 判断双栈是否为空 int isDoubleStackEmpty(DoubleStack *s) { return isStack1Empty(s) && isStack2Empty(s); } // 判断1是否已满 int isStack1Full(DoubleStack *s) { return s->top1 + 1 == s->top2; } // 判断2是否已满 int isStack2Full(DoubleStack *s) { return s->top2 - 1 == s->top1; } // 判断双栈是否已满 int isDoubleStackFull(DoubleStack *s) { return isStack1Full(s) || isStack2Full(s); } // 入操作,stackFlag表示是往哪个中入(1为1,2为2) int push(DoubleStack *s, int stackFlag, int value) { if (isDoubleStackFull(s)) { printf("Double Stack is Full.\n"); return 0; } if (stackFlag == 1) { s->data[++s->top1] = value; } else if (stackFlag == 2) { s->data[--s->top2] = value; } else { printf("Invalid Stack Flag.\n"); return 0; } return 1; } // 出操作,stackFlag表示是从哪个中出(1为1,2为2) int pop(DoubleStack *s, int stackFlag, int *value) { if (isDoubleStackEmpty(s)) { printf("Double Stack is Empty.\n"); return 0; } if (stackFlag == 1) { if (isStack1Empty(s)) { printf("Stack1 is Empty.\n"); return 0; } *value = s->data[s->top1--]; } else if (stackFlag == 2) { if (isStack2Empty(s)) { printf("Stack2 is Empty.\n"); return 0; } *value = s->data[s->top2++]; } else { printf("Invalid Stack Flag.\n"); return 0; } return 1; } int main() { DoubleStack s; initDoubleStack(&s); push(&s, 1, 1); push(&s, 1, 2); push(&s, 1, 3); push(&s, 2, 4); push(&s, 2, 5); push(&s, 2, 6); int value; pop(&s, 1, &value); printf("pop from stack1: %d\n", value); pop(&s, 2, &value); printf("pop from stack2: %d\n", value); return 0; } ``` 以上代码实现双栈结构的初始化、判断是否为空、判断是否已满、入和出操作。可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

润松3344

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值