将栈和队列封装成模板类
栈:
- #include
- #define N 128
-
- using namespace std;
-
- template <typename T>
- class My_stack{
- private:
- T data[N];
- int top;
- public:
- //无参构造
- My_stack();
-
- //判空
- bool empty()const;
-
- //入栈
- void push(const T val);
-
- //出栈
- void pop();
-
- //求栈中元素个数
- int size();
-
- //求栈顶元素
- T& My_top();
-
- //遍历
- void show();
- };
-
- //无参构造
- template <typename T>
- My_stack
::My_stack():top(-1){} -
- //判空
- template <typename T>
- bool My_stack
::empty()const{ - return top == -1?1:0;
- }
-
- //入栈
- template <typename T>
- void My_stack
::push(const T val){ - if(top == N){cout<<"栈满"<
return;} - top++;
- data[top] = val;
- }
-
- //出栈
- template <typename T>
- void My_stack
::pop(){ - if(empty()){cout<<"栈空"<
return;} - data[top] = 0;
- top--;
- }
-
- //求栈顶元素
- template <typename T>
- T& My_stack
::My_top(){ - if(empty()){cout<<"栈空"<
- return data[top];
- }
-
- //求栈中元素个数
- template <typename T>
- int My_stack
::size(){ - cout<<"栈中元素个数为:";
- return top+1;
- }
-
- //遍历
- template <typename T>
- void My_stack
::show(){ - if(empty()){cout<<"栈空"<
return;} - for(int i=0;i
1;i++){ - cout<" ";
- }
- cout<
- }
-
-
- int main()
- {
- My_stack<int> s;
- for(int i=0;i<10;i++){
- s.push(i);
- }
- s.show();
- cout<
size()< - cout<<"栈顶元素为:"<
My_top()< -
- for(int j=0;j<5;j++){
- s.pop();
- }
- s.show();
- cout<
size()< - cout<<"栈顶元素为:"<
My_top()< -
-
-
- return 0;
- }

队列:
- #include
- #define N 9
-
- using namespace std;
-
- template <typename T>
- class My_queuse{
- private:
- T data[N];
- int front,tail;
- int len;
- public:
- //无参构造
- My_queuse();
-
- //判空
- bool empty()const;
-
- //判满
- bool full()const;
-
- //入队
- void push(const T &val);
-
- //出队
- void pop();
-
- //求队中元素个数
- int size();
-
- //求队头元素
- T& My_front();
-
- //遍历
- void show();
- };
-
- //无参构造
- template <typename T>
- My_queuse
::My_queuse():front(0),tail(0),len(0){} -
-
- //判空
- template <typename T>
- bool My_queuse
::empty()const{ - if(tail == front){
- return true;
- }
- return false;
- }
-
- //判满
- template <typename T>
- bool My_queuse
::full()const{ - if((tail+1)%N == front){
- return true;
- }
- return false;
- }
-
- //入队
- template <typename T>
- void My_queuse
::push(const T &val){ - if(full()){cout<<"队满"<
return;} - data[tail] = val;
- tail = (tail+1)%N;
- len++;
- }
-
- //求队头元素
- template <typename T>
- T& My_queuse
::My_front(){ - if(empty()){cout<<"队满"<
- return data[front];
- }
-
- //出队
- template <typename T>
- void My_queuse
::pop(){ - if(empty()){return;}
- data[front] = 0;
- front = (front+1)%N;
- len--;
- return;
- }
-
- //队内元素个数
- template <typename T>
- int My_queuse
::size(){ - return len;
- }
-
- //遍历
- template <typename T>
- void My_queuse
::show(){ - if(empty()){return;}
- int i = front;
- while(1){
- cout<" ";
- if((front+1)%N==tail){
- break;
- }
- front = (front+1)%N;
- }
- front = i;
- cout <
- }
-
-
- int main()
- {
-
- My_queuse<int> q;
- for(int i=0;i<8;i++){
- q.push(i+1);
- }
- q.show();
- cout<<"队长为:"<
size()< - for(int j=0;j<4;j++){
- q.pop();
- }
- q.show();
- cout<<"队长为:"<
size()< - cout<<"队头元素:"<
My_front()< -
- return 0;
- }

-
相关阅读:
【编程题】【Scratch三级】2022.06 古堡历险记
优雅的使用String字符串处理各种类型转换
linux之基础shell脚本编程4 字符串操作,变量赋值,配置用户环境
在家怎么做芋圆 芋圆的做法
STM32CubeMX安装、使用、配置
[Python]面向对象基础
【毕业设计】基于php+mysql的成绩查询系统设计与实现(毕业论文+程序源码)——成绩查询系统
java多线程文件下载器
TPA3045-ASEMI光伏二极管TPA3045
Linux--线程 创建、等待、退出
-
原文地址:https://blog.csdn.net/2301_77665369/article/details/132864059
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU