用模板类实现栈与队列的数据结构

1 栈

#include <iostream>

using namespace std;

template<typename T1>
class Stack {
	T1* top;  //栈顶指针
	T1* base; //栈底指针
	int Maxsize; //栈的最大容量
public:
	Stack() :top(nullptr), base(nullptr), Maxsize(0) {} //无参构造

	//栈的初始化
	void Init(int Max)
	{
		Maxsize = Max;
		base = new T1[Maxsize];  //申请栈的空间
		top = base;				//初始状态,两者相等
	}
	//入栈
	void insert_stack(T1 data)
	{
		//先判断栈的空间
		if ((top == nullptr) || ((top - base) >= Maxsize))
		{
			cout << "The stack does not exist or is full" << endl;
		}
		else
		{
			*top = data;  //先入
			top += 1;  //后自增
			cout << "Stacked successfully" << endl;
		}
	}
	//出栈
	void out_stack()
	{
		//判断栈空间
		if ((top == nullptr) || ((top - base) == 0))
		{
			cout << "Stack does not exist or has no elements" << endl;
		}
		else
		{
			//*top = 0; //可以选择在删除该元素前,赋一个默认值
			cout << "out:" << *top << endl; //出栈前打印要出栈的元素
			top -= 1;
			cout << "Successfully exited the stack" << endl;
		}
	}
	//遍历栈
	void ergodic_stack()
	{
		if (top == nullptr || ((top - base) == 0))
		{
			cout << "Stack does not exist or has no elements" << endl;
			//cout << "Please initialize the stack first" << endl; //提示初始化栈
		}
		else
		{
			for (int i = 0; i < (top - base); i++)
			{
				cout << base[i] << endl;
			}
		}
	}
	//清空栈
	void clear_stack()
	{
		//判断栈空间
		if ((top == nullptr) || ((top - base) == 0))
		{
			cout << "Stack does not exist or has no elements" << endl;
		}
		else
		{
			base = top; //两者相等,即可达到清空栈的效果
			cout << "clear success" << endl;
		}
	}
	//销毁栈
	void destroy_stack()
	{
		//栈不存在
		if (base == nullptr)
		{
			cout << "Stack does not exist" << endl;
		}
		else
		{
			delete[]base;
			base = NULL;
			top = NULL;
			cout << "Destroyed successfully" << endl;
		}
	}
};


1.1 main

int main(void)
{
	Stack<char>s1;
	//s1.destroy_stack();
	s1.Init(10); //先初始化栈

	s1.insert_stack(65);
	s1.insert_stack(66);
	s1.insert_stack(67); //入栈

	//遍历栈
	s1.ergodic_stack();
	//出栈
	s1.out_stack();
	s1.out_stack();//先进后出
	//遍历栈
	s1.ergodic_stack();
	//清空栈
	s1.clear_stack();
	//遍历栈
	s1.ergodic_stack();
	return 0;
}

1.2 结果视图 

 2 队列

#include <iostream>

using namespace std;

template<typename T1>
class Queue {
	T1* base; //用来创建队列
	int front; //队列的头
	int rear;	//队列的尾
	int Maxsize; //队列的容量
public:
	Queue():base(nullptr), front(0), rear(0){}	//无参构造
	//初始化队列
	void Init(int Max)
	{
		Maxsize = Max;
		base = new T1[Maxsize];
	}
	//入队
	void insert_queue(T1 data)
	{
		//先判断合法性
		if (base == nullptr || ((rear + 1) % Maxsize) == front)
		{
			cout << "The queue does not exist or is full" << endl;
		}
		else
		{
			base[rear] = data;
			//由于是循环队列,不能使用++,否则将无法再次入队
			rear = (rear + 1) % Maxsize; //巧妙地运用取余,加以循环;这样使得最大的存储元素个数等于最大容量-1
			cout << "Successfully joined the queue" << endl;
		}
	}
	//出队
	void out_queue()
	{
		//先判断合法性
		if (base == nullptr || rear == front)
		{
			cout << "The queue does not exist or has no elements" << endl;
		}
		else
		{
			//出队前,先展示一下出队元素
			cout << "out:" << base[front] << endl;
			front = (front + 1) % Maxsize; //与入队同理
			cout << "Successfully exited the queue" << endl;
		}
	}
	//遍历队列
	void ergodic_queue()
	{
		//先判断合法性
		if (base == nullptr || rear == front)
		{
			cout << "The queue does not exist or has no elements" << endl;
		}
		else
		{
			int i = front; //从头部位置开始
			while (i != rear)
			{
				cout << base[i] << endl;
				i = (i + 1) % Maxsize;  //与入队同理
			}
		}
	}
	//清空队列
	void clear_queue()
	{
		//先判断合法性
		if (base == nullptr || rear == front)
		{
			cout << "The queue does not exist or has no elements" << endl;
		}
		else
		{
			front = rear; //两者相等即可达到清空队列效果
			cout << "clear success" << endl;
		}
	}
	//销毁队列
	void destroy_queue()
	{
		delete[]base;
		base = nullptr;
		cout << "Queue destroyed successfully" << endl;
	}
};

2.1 main

int main(void)
{
	Queue<int>q1;
	q1.Init(8); //初始化队列的大小

	q1.insert_queue(15);
	q1.insert_queue(16);
	q1.insert_queue(17);
	q1.insert_queue(18); //入队
	//遍历队列
	q1.ergodic_queue();
	//出队
	q1.out_queue();
	q1.out_queue();//遵循先进先出原则
	//遍历队列
	q1.ergodic_queue();
	//清空队列
	q1.clear_queue();
	//遍历队列
	q1.ergodic_queue();
	return 0;
}

2.2 结果视图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值