当前位置: 首页 > news >正文

c++day3

stack.h

#ifndef STACK_H
#define STACK_H
#include <iostream>
//#define max 128
using namespace std;
class Stack
{
private:int* stack;//数组指针int top;//栈顶元素int max;//栈容量
public://构造函数Stack();//析构函数~Stack();//定义拷贝构造函数Stack(const Stack &other);//入栈void push(int item);//出栈int pop();//清空栈void clear();//遍历栈void stack_show();//判空bool empty();//判满bool full();//获取栈顶元素int stack_top();//求栈的大小int stack_size();
};
#endif // STACK_H

stack.c

#include"stack.h"//构造函数
Stack::Stack():stack(nullptr),top(-1),max(128)
{stack = new int[max];for(int i=0;i<max;i++){stack[i]=0;}
}
//拷贝函数
Stack::Stack(const Stack& other)
{max = other.max;stack = new int[other.max];for(int i = 0; i < other.max; i++){stack[i] = other.stack[i];}
}//入栈
void Stack::push(int item)
{if (full()){cout << "Stack is full." << endl;return;}stack[++top] = item;cout << "push success" << endl;
}//出栈
int Stack::pop()
{if (empty()){cout << "Stack is empty." << endl;return -1;}int item = stack[top--];return item;
}
//清空栈
void Stack::clear()
{top = -1;
}
//遍历栈
void Stack::stack_show()
{for(int i=0;i<top+1;i++){cout<<stack[i]<<endl;}
}//判空
bool Stack::empty()
{return top == -1;
}//判满
bool Stack::full()
{return top == max-1;
}
//获取栈顶元素
int Stack::stack_top()
{if (empty()){cout << "Stack is empty." << endl;return -1;}return stack[top];
}
//求栈的大小
int Stack::stack_size()
{return top + 1;
}

 queue.h

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;class Queue
{
public://构造函数Queue(int size);//析构函数~Queue();//拷贝函数Queue(const Queue& other);//入队bool enqueue(int a);//出队bool dequeue();//清空void clear();//判空bool isEmpty();//判满bool isFull();//获取队列大小int getSize();private:int* queue;//队列数组int size;//队列大小int front;//队头int rear;//队尾
};#endif // QUEUE_H

 queue.c

#include"queue.h"Queue::Queue(int size)
{this->size = size;queue = new int[size];         // 根据队列容量动态分配数组内存front = -1;                        // 初始化队头位置为-1rear = -1;                         // 初始化队尾位置为-1
}Queue::~Queue()
{delete queue;                    // 释放数组内存空间
}Queue::Queue(const Queue& other)
{size = other.size;          // 复制队列容量queue = new int[size];          // 根据队列容量动态分配数组内存front = other.front;                // 复制队头位置rear = other.rear;                  // 复制队尾位置for (int i = front; i != rear; i = (i + 1) % size){queue[i] = other.queue[i];      // 复制数组元素}queue[rear] = other.queue[rear];    // 复制数组元素
}bool Queue::enqueue(int a)
{if (isFull())                       // 判断队列是否已满{cout << "Queue is full." << endl;    // 输出错误信息return false;                                  // 入队失败,返回false}if (isEmpty())                      // 如果队列为空{front = 0;                      // 更新队头位置为0}rear = (rear + 1) % size;        // 更新队尾位置,考虑循环queue[rear] = a;                  // 将元素a入队return true;                         // 入队成功,返回true
}bool Queue::dequeue()
{if (isEmpty())                      // 判断队列是否为空{cout << "Queue is empty." << endl;    // 输出错误信息return false;                                  // 出队失败,返回false}if (front == rear)                  // 如果队列中只有一个元素{front = -1;                     // 更新队头位置为-1rear = -1;                      // 更新队尾位置为-1}else{front = (front + 1) % size;  // 更新队头位置,考虑循环}return true;                        // 出队成功,返回true
}void Queue::clear()
{front = -1;                         // 清空队列,更新队头位置为-1rear = -1;                          // 清空队列,更新队尾位置为-1
}bool Queue::isEmpty()
{return front == -1 && rear == -1;   // 判断队列是否为空,根据队头位置和队尾位置是否都为-1
}bool Queue::isFull()
{return (rear + 1) % size == front;    // 判断队列是否已满,根据队尾位置加1取模后是否等于队头位置
}int Queue::getSize()
{if (isEmpty())                      // 如果队列为空{return 0;                       // 返回队列大小为0}if (front <= rear)                  // 如果队头位置小于等于队尾位置{return rear - front + 1;        // 返回队列大小为队尾位置减去队头位置再加1}else{return size - front + rear + 1;   // 返回队列大小为队列容量减去队头位置再加上队尾位置再加1}
}

http://www.lryc.cn/news/164629.html

相关文章:

  • 算法通过村第六关-树青铜笔记|中序后序
  • C++动态内存管理+模板
  • SQL 注入漏洞攻击
  • 一篇五分生信临床模型预测文章代码复现——Figure 10.机制及肿瘤免疫浸润(四)
  • Transformer 模型中常见的特殊符号
  • C# halcon SubImage的使用
  • 每天几道Java面试题:异常机制(第三天)
  • Linux 中的 chattr 命令及示例
  • LeetCode 2605. Form Smallest Number From Two Digit Arrays【数组,哈希表,枚举;位运算】1241
  • VoxWeekly|The Sandbox 生态周报|20230904
  • antd setFieldsValue 设置初始值无效AutoComplete 设置默认值失败
  • 01-Redis核心数据结构与高性能原理
  • 预防Dos攻击
  • ant design的文档真的是一坨屎
  • 关于迁移学习的一点理解
  • 【力扣周赛】第 361 场周赛(⭐前缀和+哈希表 树上倍增、LCA⭐)
  • 解决 Android 依赖冲突
  • 前端设计模式基础笔记
  • Python项目开发:Flask基于Python的天气数据可视化平台
  • Dell 服务器常见报错信息汇总
  • 算法通关村-----贪心面试大热门之区间问题
  • OAK相机:自动或手动设置相机参数
  • 百家宴焕新上市,持续深耕100-300元价位段
  • Linux Debian12使用git将本地项目上传到码云(gitee)远程仓库
  • 电子烟行业常用的英文表达
  • 【SpringMvc 丨跨域】
  • 【C语言】【strlen函数的使用与模拟实现】
  • 类和对象【基础概念】
  • 如何测试生成式人工智能(AIGC)
  • 机器学习算法详解3:逻辑回归