C++day3

1> 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量

成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小

头文件

cpp 复制代码
#ifndef STACK_H
#define STACK_H
#include <iostream>
#define max 32

using namespace std;
class Stack
{
private:
    int data[max];
    int top=-1;
public:
    Stack();
    ~Stack();
    Stack(Stack &s);
    bool stack_empty();
    bool stack_full();
    int stack_push(int e);
    int stack_pop();
    int stack_top();
    int stack_size();
    void stack_clear();
};



#endif // STACK_H

源文件

cpp 复制代码
#include <stack.h>
Stack::Stack()
{
    cout<<"Stack:构造函数"<<endl;
}
Stack::~Stack()
{
    cout<<"Stack:析构函数"<<endl;
}
Stack::Stack(Stack &s):top(s.top)
{
    for(int i=0;i<top;i++){
        data[i]=s.data[i];
    }
    cout<<"Stack:拷贝构造函数"<<endl;
}
bool Stack::stack_empty()
{
    if(-1==top)
        return true;
    else
        return false;
}
bool Stack::stack_full()
{
    if(max-1==top)
        return true;
    else
        return false;
}
int Stack::stack_push(int e)
{
    top++;
    data[top]=e;
    cout<<e<<"入栈成功"<<endl;
    return 1;
}
int Stack::stack_pop()
{
    int e=data[top];
    top--;
    cout<<e<<"出栈成功"<<endl;
    return 1;
}
int Stack::stack_top()
{
    int e=data[top];
    cout<<"栈顶元素为:"<<e<<endl;
    return 1;
}
int Stack::stack_size()
{
    cout<<"栈的大小为:"<<top+1<<endl;
    return 1;
}
void Stack::stack_clear()
{
    top=-1;
    cout<<"栈已清空"<<endl;
}

测试文件

cpp 复制代码
#include <iostream>
#include <stack.h>
using namespace std;

int main()
{
    Stack s;
    s.stack_push(1);
    s.stack_push(2);
    s.stack_push(3);
    s.stack_push(4);
    s.stack_size();
    s.stack_top();
    s.stack_pop();
    s.stack_pop();
    s.stack_pop();
    s.stack_pop();
    s.stack_size();
    s.stack_top();
    s.stack_clear();
    return 0;
}

测试结果

2> 自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置

成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小

头文件

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

using namespace std;
#define max 32
class Queue
{
private:
    int data[max];
    int front;
    int tail;
public:
    Queue();
    ~Queue();
    Queue(Queue &q);
    bool queue_empty();
    bool queue_full();
    int queue_push(int e);
    int queue_pop();
    int queue_size();
    void queue_clear();


};

#endif // QUEUE_H

源文件

cpp 复制代码
#include <queue.h>
Queue::Queue():front(0),tail(0)
{
    cout<<"Queue:构造函数"<<endl;
}

Queue::~Queue()
{
    cout<<"Queue:析构函数"<<endl;
}
Queue::Queue(Queue &q):front(q.front),tail(q.tail)
{
    while(front!=tail){
        data[front]=q.data[front];
        front=(front+1)%max;
    }
}
bool Queue::queue_empty()
{
    if(front==tail)
        return true;
    else
        return false;
}
bool Queue::queue_full()
{
    if((tail+1)%max==front)
        return true;
    else
        return false;
}
int Queue::queue_push(int e)
{
    if(Queue::queue_full()){
        cout<<"入队失败"<<endl;
        return -1;
    }
    data[tail]=e;
    tail=(tail+1)%max;
    cout<<e<<"入队成功"<<endl;
    return 1;
}
int Queue::queue_pop()
{
    if(Queue::queue_empty()){
        cout<<"出队失败"<<endl;
        return -1;
    }
    front=(front+1)%max;
    cout<<"出队成功"<<endl;
    return 1;
}
int Queue::queue_size()
{
    int size=(tail-front+max)%max;
    cout<<"队长为:"<<size<<endl;
    return size;
}
void Queue::queue_clear()
{
    front=tail;
    cout<<"清空队列"<<endl;
    return;
}

测试文件

cpp 复制代码
#include <iostream>
#include <queue.h>
using namespace std;

int main()
{
    Queue q;
    q.queue_push(1);
    q.queue_push(2);
    q.queue_push(3);
    q.queue_push(4);
    q.queue_size();
    q.queue_pop();
    q.queue_pop();
    q.queue_pop();
    q.queue_pop();
    q.queue_size();
    q.queue_clear();
    return 0;
}

测试结果

思维导图

相关推荐
ChinaRainbowSea5 分钟前
补充:问题:CORS ,前后端访问跨域问题
java·spring boot·后端·spring
KiddoStone14 分钟前
多实例schedule job同步数据流的数据一致性设计和实现方案
java
算法_小学生35 分钟前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode
岁忧35 分钟前
(LeetCode 每日一题) 1865. 找出和为指定值的下标对 (哈希表)
java·c++·算法·leetcode·go·散列表
YuTaoShao39 分钟前
【LeetCode 热题 100】240. 搜索二维矩阵 II——排除法
java·算法·leetcode
whoarethenext1 小时前
使用 C++ 实现 MFCC 特征提取与说话人识别系统
开发语言·c++·语音识别·mfcc
R-G-B1 小时前
【MFC】Combobox下拉框中4个选项,运行后点击下拉框选项不能全部展示出来,只能显示2个选项,需要垂直滚动条滚动显示其余选项
c++·mfc
考虑考虑1 小时前
JDK9中的dropWhile
java·后端·java ee
想躺平的咸鱼干2 小时前
Volatile解决指令重排和单例模式
java·开发语言·单例模式·线程·并发编程
hqxstudying2 小时前
java依赖注入方法
java·spring·log4j·ioc·依赖