C++数据结构类的自实现,封装栈,循环队列

my_Queue.h

#ifndef MY_QUEUE_H
#define MY_QUEUE_H

class My_Queue
{
    
    
private:
    int* 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

my_Queue.c

#include "my_Queue.h"
#include <iostream>

using namespace std;

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

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

    m_length = len+1;
}

My_Queue::My_Queue(const My_Queue& obj)
{
    
    
    m_length = obj.m_length;

    m_queue = new int[obj.m_length];

    for(int i = 0; i < obj.m_length; i++)
    {
    
    
        m_queue[i] = obj.m_queue[i];
    }
}

int My_Queue::length()
{
    
    
    return (tail + m_length - front) % m_length;
}

bool My_Queue::pop()
{
    
    
    bool ret = true;

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

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

    return ret;
}

bool My_Queue::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;
}

bool My_Queue::empty()
{
    
    
    return front == tail;
}

bool My_Queue::full()
{
    
    
    return (tail + 1) % m_length == front;
}

bool My_Queue::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;
}

My_Queue::~My_Queue()
{
    
    
    delete[] m_queue;
}

在这里插入图片描述

my_Stack.h

#ifndef MY_STACK_H
#define MY_STACK_H

class My_Stack
{
    
    
private:
    int* 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

my_Stack.c

#include "my_Stack.h"
#include <iostream>

using namespace std;

My_Stack::My_Stack(int len):m_stack(nullptr),m_length(0),m_max(0)
{
    
    
    m_stack = new int[len];

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

    m_max = len;
}

My_Stack::My_Stack(const My_Stack& obj)
{
    
    
    m_max = obj.m_max;

    m_stack = new int[obj.m_max];

    for(int i = 0; i < obj.m_max; i++)
    {
    
    
        m_stack[i] = obj.m_stack[i];
    }
}

int My_Stack::length()
{
    
    
    return m_length;
}

int My_Stack::gettop()
{
    
    
    if(empty())
    {
    
    
        return -1;
    }

    int &top = m_stack[m_length-1];

    return top;
}

bool My_Stack::pop()
{
    
    
    bool ret = true;

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

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

    return ret;
}

bool My_Stack::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;
}

bool My_Stack::empty()
{
    
    
    return m_length == 0;
}

bool My_Stack::full()
{
    
    
    return m_length == m_max;
}

bool My_Stack::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;
}

bool My_Stack::clear()
{
    
    
    bool ret = true;

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

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

    return ret;
}

My_Stack::~My_Stack()
{
    
    
    delete[] m_stack;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_72847002/article/details/132767094