day3_C++

day3_C++

  • 思维导图
  • [用C++的类完成数据结构 栈的相关操作](#用C++的类完成数据结构 栈的相关操作)
  • [用C++的类完成数据结构 循环队列的相关操作](#用C++的类完成数据结构 循环队列的相关操作)

思维导图

用C++的类完成数据结构 栈的相关操作

stack.h

复制代码
#ifndef STACK_H
#define STACK_H

#include <iostream>
#include <cstring>

using namespace std;


typedef int datatype;

#define MAX 5

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

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


private:
    int top;
    datatype *data;
};

#endif // STACK_H

stack.cpp

复制代码
#include "stack.h"

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


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

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


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

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


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


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

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

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

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

用C++的类完成数据结构 循环队列的相关操作

queue.h

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

using namespace std;

typedef int datatype;
#define MAX 5

class Queue
{

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

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


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

#endif // QUEUE_H

queue.cpp

复制代码
#include "queue.h"

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


Queue::Queue(const Queue& others):data(new int[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;
}

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

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

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


void Queue::in_queue(datatype e)
{
    if(this->is_full() == true){
        cout<<"队列满了"<<endl;
        return ;
    }
    this->data[this->tail] = e;
    this->tail =  (this->tail+1)%MAX;
    cout<<"入队成功"<<endl;
}
/*出队*/
datatype Queue::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;
}


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

void Queue::get_queueSize()
{
    cout<<"队列的大小是:" <<(this->tail-this->font+MAX)%MAX<<endl;
}
相关推荐
橘颂TA几秒前
【Linux】从 “抢资源” 到 “优雅控场”:Linux 互斥锁的原理与 C++ RAII 封装实战(Ⅰ)
linux·运维·服务器·c++·算法
菩提祖师_2 分钟前
量子机器学习在时间序列预测中的应用
开发语言·javascript·爬虫·flutter
刘97532 分钟前
【第22天】22c#今日小结
开发语言·c#
明天好,会的9 分钟前
分形生成实验(三):Rust强类型驱动的后端分步实现与编译时契约
开发语言·人工智能·后端·rust
YanDDDeat12 分钟前
【JVM】类初始化和加载
java·开发语言·jvm·后端
枫叶丹416 分钟前
【Qt开发】Qt系统(三)->事件过滤器
java·c语言·开发语言·数据库·c++·qt
wjs202417 分钟前
CSS Position(定位)
开发语言
亓才孓19 分钟前
继承父类和接口,又冲突的变量名怎么解决
java·开发语言
superman超哥29 分钟前
Rust API 设计的零成本抽象原则:性能与表达力的完美统一
开发语言·后端·rust·rust api·抽象原则·性能与表达力
云栖梦泽31 分钟前
Rust语言:系统级编程的新时代选择
开发语言