day6_C++

day6_C++

  • [模板 栈](#模板 栈)
  • [模板 队列](#模板 队列)
  • 思维导图

模板 栈

stack.h

复制代码
#ifndef STACK_H
#define STACK_H

#include <iostream>
#include <cstring>

using namespace std;


#define MAX 5

template<typename T>
class Stack
{
public:
    /*构造函数*/
    Stack();
    /*拷贝构造函数*/
    Stack(const Stack& others);
    /*析构函数*/
    ~Stack();

    /*判满 true 满 */
    bool is_full();
    /*判满 true 空*/
    bool is_empty();
    /*入栈*/
    void in_stack(T e);
    /*出栈*/
    T out_stack();
    /*清空栈*/
    void clear_stack();
    /*求栈顶元素*/
    T get_stackTop_E();
    /*求栈的大小*/
    void get_stackSize();


private:
    int top;
    T *data;
};

#endif // STACK_H

stack.c

复制代码
#include "stack.h"

template<typename T>
Stack<T>::Stack():data(new T[MAX]),top(-1)
{
    memset(this->data,0,MAX);
    //在堆区申请max个int大小的空间
    cout<<"栈容器初始化成功"<<endl;
}

template<typename T>
Stack<T>::Stack(const Stack& others):data(new T[MAX]),top(others.top)
{
    //深拷贝,将堆区内容也拷贝进来
    for(int i = 0;i<MAX-1;i++){
        this->data[i] = others.data[i];
    }
    cout<<"拷贝完成"<<endl;
}

template<typename T>
Stack<T>::~Stack()
{
    //释放堆区数据
    delete []data;
    cout<<"析构完成"<<endl;
}

template<typename T>
bool Stack<T>::is_full()
{
    if(this->top ==MAX-1)
        return true;
    else
        return false;
}

template<typename T>
bool Stack<T>::is_empty()
{
    if(this->top == -1)
        return true;
    else
        return false;
}

template<typename T>
void Stack<T>::in_stack(T e)
{
    if(this->is_full()==false){
        this->top++;
        this->data[this->top] = e;
        cout<<"入栈成功"<<endl;
    }else{
        cout<<"入栈失败,栈满"<<endl;
    }
}

template<typename T>
T Stack<T>::out_stack()
{
    if(this->is_empty()==false){
        T temp = this->data[this->top];
        this->top--;
        return temp;
    }else{
         cout<<"出栈失败,栈空"<<endl;
         return NULL;
    }
}

template<typename T>
void Stack<T>::clear_stack()
{
    if(this->is_empty()==false){
        this->top=-1;
        cout<<"清空成功"<<endl;
    }else{
         cout<<"栈空,无需清理"<<endl;
    }
}

template<typename T>
T Stack<T>::get_stackTop_E()
{
     if(this->is_empty()==true)
         return NULL;
     return this->data[this->top];
}

template<typename T>
void Stack<T>::get_stackSize(){
    cout<<"栈中有元素 "<<this->top+1<<"个"<<endl;

}

模板 队列

queue.c

复制代码
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include <cstring>

using namespace std;

#define MAX 5

template<typename T>
class Queue
{

public:
    /*构造函数*/
    Queue();
    /*拷贝构造函数*/
    Queue(const Queue<T>& others);
    /*析构函数*/
    ~Queue();

    /*判满 true 满 */
    bool is_full();
    /*判满 true 空*/
    bool is_empty();
    /*入队*/
    void in_queue(T e);
    /*出队*/
    T out_queue();
    /*清空队*/
    void clear_queue();
    /*求队的大小*/
    void get_queueSize();


private:
    T *data;
    int font;
    int tail;
};

#endif // QUEUE_H

queue.c

复制代码
#include "queue.h"

template<typename T>
Queue<T>::Queue():data(new T [MAX]),tail(0),font(0)
{
    memset(this->data,0,MAX);
    cout<<"循环队列初始化成功"<<endl;
}

template<typename T>
Queue<T>::Queue(const Queue<T>& others):data(new T[MAX]),font(others.font),tail(others.tail)
{
    //深拷贝
    int f = this->font;
    int t = this->tail;
    while ((f+MAX)%MAX==t) {
        this->data[f] = others.data[f];
        f++;
    }
    cout<<"拷贝完成"<<endl;
}

template<typename T>
bool Queue<T>::is_full()
{
    if((this->tail+1)%MAX == this->font){
        return true;
    }
    return false;
}

template<typename T>
bool Queue<T>::is_empty()
{
    if(this->font == this->tail){
        return true;
    }
    return false;
}

template<typename T>
Queue<T>::~Queue()
{
    //释放堆区数据
    delete []data;
    cout<<"析构完成"<<endl;
}

template<typename T>
void Queue<T>::in_queue(T e)
{
    if(this->is_full() == true){
        cout<<"队列满了"<<endl;
        return ;
    }
    this->data[this->tail] = e;
    this->tail =  (this->tail+1)%MAX;
    cout<<"入队成功"<<endl;
}

/*出队*/
template<typename T>
T Queue<T>::out_queue()
{
    if(this->is_empty() == true){
        cout<<"队列空,无元素"<<endl;
        return NULL;
    }
    int temp =  this->data[this->font];
    this->font = (this->font+1)%MAX;
    return temp;
}

template<typename T>
void Queue<T>::clear_queue()
{
    if(this->is_empty() == true){
        cout<<"队列空,无元素"<<endl;
        return;
    }
    this->font = 0;
    this->tail = 0;
}

template<typename T>
void Queue<T>::get_queueSize()
{
    cout<<"队列的大小是:" <<(this->tail-this->font+MAX)%MAX<<endl;
}

思维导图

相关推荐
程序员弘羽20 分钟前
C++ 第四阶段 内存管理 - 第二节:避免内存泄漏的技巧
java·jvm·c++
【ql君】qlexcel27 分钟前
Notepad++ 复制宏、编辑宏的方法
开发语言·javascript·notepad++··宏编辑·宏复制
爱思德学术33 分钟前
中国计算机学会(CCF)推荐学术会议-B(交叉/综合/新兴):BIBM 2025
算法
Zevalin爱灰灰36 分钟前
MATLAB GUI界面设计 第六章——常用库中的其它组件
开发语言·ui·matlab
冰糖猕猴桃43 分钟前
【Python】进阶 - 数据结构与算法
开发语言·数据结构·python·算法·时间复杂度、空间复杂度·树、二叉树·堆、图
lifallen1 小时前
Paimon vs. HBase:全链路开销对比
java·大数据·数据结构·数据库·算法·flink·hbase
wt_cs1 小时前
银行回单ocr api集成解析-图像文字识别-文字识别技术
开发语言·python
_WndProc2 小时前
【Python】Flask网页
开发语言·python·flask
liujing102329292 小时前
Day04_刷题niuke20250703
java·开发语言·算法
DolphinDB2 小时前
如何在C++交易系统中集成高性能回测与模拟撮合
c++