
用模板实现栈
- #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;
- }
- }
-
相关阅读:
密码安全策略
MPP(无主备)环境搭建
常见的限流算法- python版本
这几个必备的vscode插件,你安装了几个
向新而生保业务增长,亚信科技持续锻造“核心引擎”
java线程池 面试题(精简)
vue制作页面水印
【沐风老师】怎么在3DMAX中使用MAXScript脚本动画编程?
dll动态链接库及ocx activex 控件regsvr32注册失败 解决方法(Win10)
go自建线程池执行任务
-
原文地址:https://blog.csdn.net/mcslll/article/details/132863957
-
最新文章
-
沪漂五周年了:我越来越迷茫了
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