1.栈的源代码:
- #include
- using namespace std;
-
- template<typename T>
- class Stack
- {
- private:
- T* arr;
- int top;
- int max;
-
- public:
- // 无参构造
- Stack() : arr(nullptr), top(-1), max(0) {}
-
- // 有参构造
- Stack(int size)
- {
- if (size > 0)
- {
- max = size;
- arr = new T[max];
- top = -1;
- cout << "栈容量设置成功" << endl;
- }
- else
- {
- cout << "输入有误,栈的容量必须大于0" << endl;
- }
- }
-
- // 拷贝构造
- Stack(const Stack& other) : top(other.max), max(other.max)
- {
- if (this != &other)
- {
- arr = new T[max];
- for (int i = 0; i < top + 1; i++)
- {
- arr[i] = other.arr[i];
- }
- }
- }
-
- // 析构函数
- ~Stack()
- {
- delete[] arr;
- }
-
- // 入栈
- void push(T value)
- {
- if (!isFull())
- {
- arr[++top] = value;
- cout << value << " 入栈成功" << endl;
- }
- else
- {
- cout << "栈满,无法入栈" << endl;
- }
- }
-
- // 出栈
- T pop()
- {
- if (!isEmpty())
- {
- return arr[top--];
- }
- else
- {
- cout << "栈空,无法出栈" << endl;
- return T(); // 返回默认值
- }
- }
-
- // 清空栈
- void clear()
- {
- top = -1;
- }
-
- // 判空
- bool isEmpty()
- {
- return top == -1;
- }
-
- // 判满
- bool isFull()
- {
- return top == max - 1;
- }
-
- // 获取栈顶元素
- T getTop()
- {
- if (!isEmpty())
- {
- return arr[top];
- }
- else
- {
- cout << "栈空,无法获取栈顶元素" << endl;
- return T();
- }
- }
-
- // 求栈的大小
- int getSize()
- {
- return top + 1;
- }
- };
-
- int main()
- {
- Stack
stringStack(10) ; -
- stringStack.push("hello");
- stringStack.push("world");
- cout << "栈的大小为: " << stringStack.getSize() << endl;
- cout << "栈顶元素为: " << stringStack.getTop() << endl;
- cout << stringStack.pop() << " 出栈成功" << endl;
- cout << "出栈一次后栈的大小为: " << stringStack.getSize() << endl;
-
- return 0;
- }
2.队列源代码
- #include
- #define MAX 5
-
- using namespace std;
-
- template <typename T>
- class Queue{
- public:
- //构造
- Queue();
- //析构
- ~Queue();
- //拷贝构造
- Queue(const Queue &other);
- //入队
- int push(T e);
- //出队
- int pop();
- //清空队
- void clear();
- //判空
- bool empty();
- //判满
- bool full();
- //求大小
- int size();
-
- private:
- int front;
- int tail;
- T *data;
- };
-
- //构造函数
- template <typename T>
- Queue
::Queue():front(0),tail(0),data(new T[MAX]) - {
- cout<<"构造函数"<
- }
- //析构函数
- template <typename T>
- Queue
::~Queue(){ - delete []data;
- cout<<"析构"<
- }
- //拷贝构造函数
- template <typename T>
- Queue
::Queue(const Queue &other):front(other.front), - tail(other.tail),
- data(new T[MAX]){
- std::copy(other.data,other.data+MAX,data);
- cout<<"拷贝构造"<
- }
- //入队
- template <typename T>
- int Queue
::push(T e){ - if(Queue
::full()){ - cout<<"队满;"<
- return 0;
- }
- data[tail]=e;
- cout<
"入队"< - tail=(tail+1)%MAX;
-
- return 1;
- }
- //出队
- template <typename T>
- int Queue
::pop(){ - if(Queue
::empty()){ - cout<<"队空"<
- return 0;
- }
- cout<"出队"<
- front=(front+1)%MAX;
- return 1;
- }
- //清空队列
- template <typename T>
- void Queue
::clear(){ - cout<<"清空队"<
- while(!Queue
::empty()){ - Queue
::pop(); - }
- }
- //判空
- template <typename T>
- bool Queue
::empty(){ - return front==tail;
- }
- //判满
- template <typename T>
- bool Queue
::full(){ - return (tail+1)%MAX==front;
- }
- //求队列大小
- template <typename T>
- int Queue
::size(){ - cout<<this<<"队列大小:";
- return (tail-front+MAX)%MAX;
- }
-
-
- int main()
- {
- Queue<int> que;
- que.push(1);
- que.push(2);
- que.push(3);
- cout<
size()< - Queue<int> q2=que;
- cout<
size()< - Queue<int> q3=q2;
- cout<
size()< - q2.clear();
- q2.pop();
- cout<
size()< -
-
相关阅读:
yolov5训练可视化指标的含义
设计一个32*8的sram,长宽比接近1:1,求具体讲一下原理
前端发送请求,显示超时取消
typescript56-泛型接口
安全性归约(构造 1)
Qt::绘制框架-选择节点-QGraphicsScene-items
【Embedded System】裸机接口开发
Java网络开发(Session)—— 从http请求 到 cookie 到 session & 用 session控制 删改数据的权限
让学前端不再害怕英语单词(三)
计算机毕业设计java基于springboot的在线动漫平台
-
原文地址:https://blog.csdn.net/2301_78047404/article/details/132868533