
用模板实现栈
- #include
-
- using namespace std;
-
- template <typename T, int MaxSize>
- class Stack {
- public:
- //构造函数
- Stack() : top(-1) {}
- //入栈
- void push(const T& value);
- //出栈
- T pop();
- //判空
- bool empty() const;
- //判满
- bool full()const;
- //清空栈
- bool clear();
- //获取栈顶元素
- T stacktop();
- //求栈的大小
- int stacksize();
- //输出栈元素
- bool show();
- private:
- T data[MaxSize];
- int top;
- };
-
- int main() {
- Stack<int, 3> intStack;
- intStack.push(1);
- intStack.push(2);
- intStack.push(3);
- intStack.pop();
- intStack.show();
-
- Stack<double, 5> doubleStack;
- doubleStack.push(1.1);
- doubleStack.push(2.2);
- doubleStack.push(3.3);
- doubleStack.pop();
- doubleStack.show();
-
-
- return 0;
- }
-
- //入栈
- template <typename T, int MaxSize>
- void Stack
::push(const T& value) { - if (top == MaxSize - 1) {
- throw std::overflow_error("该栈已满");
- }
- data[++top] = value;
- }
- //出栈
- template <typename T, int MaxSize>
- T Stack
::pop() { - if (empty()) {
- throw std::underflow_error("该栈为空");
- }
- return data[top--];
- }
- //判空
- template <typename T, int MaxSize>
- bool Stack
::empty() const { - return top == -1;
- }
-
- //判满
- template <typename T, int MaxSize>
- bool Stack
::full()const - {
- return top = MaxSize-1;
- }
-
- //清空栈
- template <typename T, int MaxSize>
- bool Stack
::clear() - {
- if(empty()){
- cout<<"该栈为空"<
- return false;
- }else {
- top = -1;
- return true;
- }
- }
-
- //获取栈顶元素
- template <typename T, int MaxSize>
- T Stack
::stacktop() - {
- return data[top];
- }
-
- //求栈的大小
- template <typename T, int MaxSize>
- int Stack
::stacksize() - {
- return top+1;
- }
-
- //输出栈元素
- template <typename T, int MaxSize>
- bool Stack
::show() - {
- if(empty()){
- cout<<"该栈为空"<
- return false;
- }else {
- for(int i=0;i<=top; i++){
- cout<" ";
- }
- cout<
- return true;
- }
- }
用模板实现队列
- #include
- #define MAXSIZE 16
-
- using namespace std;
- template <typename T, int MaxSize>
- class LoopQueue
- {
- public:
- //构造函数
- LoopQueue():FrontIndex(0), RearIndex(0){};
- //入队列
- bool push(T value);
- //出队列并返回队列顶元素
- T pop();
- //清空队列
- bool clear();
- //判空
- bool isEmpty();
- //判满
- bool isFull();
- //求队列的大小
- int LoopQueuesize();
- //输出队列元素
- bool show();
- private:
- T Data[MaxSize];
- int FrontIndex;
- int RearIndex;
- };
-
- int main()
- {
- LoopQueue<int, 5> intL;
- intL.push(5);
- intL.push(7);
- intL.push(10);
- intL.pop();
- intL.show();
-
- LoopQueue<double, 5> doubleL;
- doubleL.push(5.5);
- doubleL.push(7.4);
- doubleL.push(10.3);
- doubleL.pop();
- doubleL.show();
-
- return 0;
- }
-
- //入队列
- template <typename T, int MaxSize>
- bool LoopQueue
::push(T value) - {
- if(isFull()){
- cout<<"该队列已满"<
- return false;
- }else{
- Data[RearIndex] = value;
- RearIndex++;
- return true;
- }
- }
-
- //出队列并返回队列顶元素
- template <typename T, int MaxSize>
- T LoopQueue
::pop() - {
- if(isEmpty()){
- throw std::underflow_error("该队列为空");
- }else {
- int Frontvalue = Data[RearIndex];
- FrontIndex++;
- return Frontvalue;
- }
- }
-
- //清空队列
- template <typename T, int MaxSize>
- bool LoopQueue
::clear() - {
- if(isEmpty()){
- cout<<"该队列为空"<
- return false;
- }else {
- FrontIndex = -1;
- RearIndex = -1;
- return true;
- }
- }
-
- //判空
- template <typename T, int MaxSize>
- bool LoopQueue
::isEmpty() - {
- return FrontIndex==RearIndex;
- }
-
- //判满
- template <typename T, int MaxSize>
- bool LoopQueue
::isFull() - {
- return (RearIndex+1)%MAXSIZE==FrontIndex;
- }
-
- //求队列的大小
- template <typename T, int MaxSize>
- int LoopQueue
::LoopQueuesize() - {
- return (RearIndex-FrontIndex+MAXSIZE)%MAXSIZE;
- }
-
- //输出队列元素
- template <typename T, int MaxSize>
- bool LoopQueue
::show() - {
- if(isEmpty()){
- cout<<"该队列为空"<
- return false;
- }else {
- int i=FrontIndex;
- cout<<"该队列为>>";
- while(1)
- {
- cout<" ";
- i++;
- if(MAXSIZE==i)
- {
- i=0;
- }
- if(i==RearIndex)
- {
- break;
- }
- }
- cout<
- return true;
- }
- }
-
相关阅读:
Git乱码
Asp .Net Core 系列:Asp .Net Core 配置 System.Text.Json
【Python】第十课 魔法方法
基于低代码平台的OA系统,更灵活高效!
linux USB摄像头 V4L2工具调试摄像头
音乐转录(AMT)库Omnizart论文笔记及实践
吴恩达机器学习笔记十四 多输出的分类 多类和多标签的区别 梯度下降优化 卷积层
Google开源依赖注入框架-Guice指南
【摆脱他人眼光的束缚】如何重视自我感受,做好自己
Go实现日志2——支持结构化和hook
-
原文地址:https://blog.csdn.net/mcslll/article/details/132863957