C++ 类模板实现栈和循环队列

栈头文件

cpp 复制代码
#ifndef MY_STACK_H
#define MY_STACK_H
template <typename T>
class My_Stack
{
private:
    T* m_stack;
    int m_length;
    int m_max;

public:
    My_Stack(int len);
    My_Stack(const My_Stack& obj);
    int length();
    int gettop();
    bool pop();
    bool push(int value);
    bool empty();
    bool full();
    bool show();
    bool clear();
    ~My_Stack();
};

#endif // MY_STACK_H

栈函数实现

cpp 复制代码
#include "my_Stack.h"
#include <iostream>

using namespace std;
template <typename T>
My_Stack<T>::My_Stack(int len):m_stack(nullptr),m_length(0),m_max(0)
{
    m_stack = new T[len];

    for(int i = 0; i < len; i++)
    {
        m_stack[i] = 0;
    }

    m_max = len;
}
template <typename T>
My_Stack<T>::My_Stack(const My_Stack& obj)
{
    m_max = obj.m_max;

    m_stack = new T[obj.m_max];

    for(int i = 0; i < obj.m_max; i++)
    {
        m_stack[i] = obj.m_stack[i];
    }
}
template <typename T>
int My_Stack<T>::length()
{
    return m_length;
}
template <typename T>
int My_Stack<T>::gettop()
{
    if(empty())
    {
        return -1;
    }

    int &top = m_stack[m_length-1];

    return top;
}
template <typename T>
bool My_Stack<T>::pop()
{
    bool ret = true;

    if(empty())
    {
        ret = false;
        cout << "栈为空" << endl;
        return ret;
    }

    m_length--;
    cout << "出栈成功" << endl;

    return ret;
}
template <typename T>
bool My_Stack<T>::push(int value)
{
    bool ret = true;

    if(full())
    {
        ret = false;
        cout << "栈已满" << endl;
        return ret;
    }

    m_stack[m_length] = value;
    m_length++;
    cout << "入栈成功" << endl;

    return ret;
}
template <typename T>
bool My_Stack<T>::empty()
{
    return m_length == 0;
}
template <typename T>
bool My_Stack<T>::full()
{
    return m_length == m_max;
}
template <typename T>
bool My_Stack<T>::show()
{
    bool ret = true;
    if(empty())
    {
        ret = false;
        cout << "栈为空" << endl;
        return ret;
    }

    for(int i = 0; i < m_length; i++)
    {
        cout << m_stack[i] << " ";
    }
    cout << endl;

    return ret;
}
template <typename T>
bool My_Stack<T>::clear()
{
    bool ret = true;

    if(empty())
    {
        ret = false;
        cout << "栈为空" << endl;
        return ret;
    }
    m_length = 0;

    cout << "栈已清空" << endl;

    return ret;
}
template <typename T>
My_Stack<T>::~My_Stack()
{
    delete[] m_stack;
}

循环队列头文件

cpp 复制代码
#ifndef MY_QUEUE_H
#define MY_QUEUE_H

template <typename T>
class My_Queue
{
private:
    T* m_queue;
    int front;
    int tail;
    int m_length;

public:
    My_Queue(int len);
    My_Queue(const My_Queue& obj);
    int length();
    bool pop();
    bool push(int value);
    bool empty();
    bool full();
    bool show();
    ~My_Queue();
};

#endif // MY_QUEUE_H

循环队列函数实现

cpp 复制代码
#include "my_Queue.h"
#include <iostream>

using namespace std;

template <typename T>
My_Queue<T>::My_Queue(int len):m_queue(nullptr),front(0),tail(0),m_length(0)
{
    m_queue = new T[len+1];

    for(int i = 0; i < len+1; i++)
    {
        m_queue[i] = 0;
    }

    m_length = len+1;
}
template <typename T>
My_Queue<T>::My_Queue(const My_Queue& obj)
{
    m_length = obj.m_length;

    m_queue = new T[obj.m_length];

    for(int i = 0; i < obj.m_length; i++)
    {
        m_queue[i] = obj.m_queue[i];
    }
}
template <typename T>
int My_Queue<T>::length()
{
    return (tail + m_length - front) % m_length;
}
template <typename T>
bool My_Queue<T>::pop()
{
    bool ret = true;

    if(empty())
    {
        cout << "队列为空" << endl;
        ret = false;
        return ret;
    }

    front = (front + 1) % m_length;
    cout << "出队成功" << endl;

    return ret;
}
template <typename T>
bool My_Queue<T>::push(int value)
{
    bool ret = true;

    if(full())
    {
        cout << "队列已满" << endl;
        ret = false;
        return ret;
    }

    m_queue[tail] = value;

    tail = (tail + 1) % m_length;
    cout << "入队成功" << endl;

    return ret;
}
template <typename T>
bool My_Queue<T>::empty()
{
    return front == tail;
}
template <typename T>
bool My_Queue<T>::full()
{
    return (tail + 1) % m_length == front;
}
template <typename T>
bool My_Queue<T>::show()
{
    bool ret = true;

    if( empty() )
    {
        ret = false;
        cout << "队列为空" << endl;
        return ret;
    }

    for(int i = front; i != tail; i = (i+1)%m_length)
    {
        cout << m_queue[i] << " ";
    }

    cout << endl;

    return ret;
}
template <typename T>
My_Queue<T>::~My_Queue()
{
    delete[] m_queue;
}
相关推荐
利刃大大4 小时前
【高并发内存池】五、页缓存的设计
c++·缓存·项目·内存池
C语言小火车4 小时前
【C++八股文】基础知识篇
c++·tcp/ip·const·智能指针·多线程同步·static关键字·c++内存模型
liulilittle5 小时前
IP校验和算法:从网络协议到SIMD深度优化
网络·c++·网络协议·tcp/ip·算法·ip·通信
眠りたいです5 小时前
基于脚手架微服务的视频点播系统-播放控制部分
c++·qt·ui·微服务·云原生·架构·播放器
Want5955 小时前
C/C++圣诞树①
c语言·开发语言·c++
老赵的博客5 小时前
c++ 杂记
开发语言·c++
jimmy.hua5 小时前
[C++刷怪笼]:set/map--优质且易操作的容器
开发语言·c++
tan180°6 小时前
Boost搜索引擎 网络库与前端(4)
linux·网络·c++·搜索引擎
bkspiderx6 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
郝学胜-神的一滴7 小时前
避免使用非const全局变量:C++中的最佳实践 (C++ Core Guidelines)
开发语言·c++·程序人生