c++day3

stack.h

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#include"queue.h"

Queue::Queue(int size)
{
    this->size = size;
    queue = new int[size];         // 根据队列容量动态分配数组内存
    front = -1;                        // 初始化队头位置为-1
    rear = -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;                     // 更新队头位置为-1
        rear = -1;                      // 更新队尾位置为-1
    }
    else
    {
        front = (front + 1) % size;  // 更新队头位置,考虑循环
    }
    return true;                        // 出队成功,返回true
}

void Queue::clear()
{
    front = -1;                         // 清空队列,更新队头位置为-1
    rear = -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
    }
}
相关推荐
CoovallyAIHub14 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
NAGNIP15 小时前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo15 小时前
半开区间和开区间的两个二分模版
算法
moonlifesudo15 小时前
300:最长递增子序列
算法
CoovallyAIHub20 小时前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub20 小时前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
聚客AI2 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v2 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工2 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农2 天前
【React用到的一些算法】游标和栈
算法·react.js