C++(day3)

思维导图:

 封装顺序栈:

stack.h

#ifndef STACK_H
#define STACK_H

#include <iostream>

using namespace std;

#define MAX 5
typedef int datatype;

class Stack{
public:
    //构造函数
    Stack();
    //析构函数
    ~Stack();
    //拷贝构造函数
    Stack(const Stack &other);
    //入栈
    int push(datatype e);
    //出栈
    int pop();
    //清空栈
    void clear();
    //判空
    bool empty();
    //判满
    bool full();
    //获取栈顶元素
    datatype topdata();
    //求栈的大小
    int size();
private:
    datatype *data;
    int top;
};

#endif // STACK_H

stack.cpp

#include"stack.h"

//构造函数
Stack::Stack():data(new datatype[MAX]),top(-1){
    cout<<"构造函数"<<endl;
}
//析构函数
Stack::~Stack(){
    delete []data;    //释放指针空间
    cout<<"析构函数"<<endl;
}
//拷贝构造函数
Stack::Stack(const Stack &other):data(new datatype[MAX]),top(other.top){
    std::copy(other.data,other.data+MAX,data);
    cout<<"拷贝函数"<<endl;
}
//入栈
int Stack::push(datatype e){
    if(Stack::full()){
        cout<<"栈满,无法入栈;"<<endl;
        return 0;
    }
    data[++top]=e;
    cout<<data[top]<<"已入栈"<<endl;
    return 1;
}
//出栈
int Stack::pop(){
    if(Stack::empty()){
        cout<<"栈空,无法出栈;"<<endl;
        return 0;
    }
    cout<<data[top--]<<"已出栈"<<endl;
    return 1;
}
//清空栈
void Stack::clear(){
    cout<<"****清空栈****"<<endl;
    while(!Stack::empty()){
        Stack::pop();
    }
}
//判空
bool Stack::empty(){
    return -1 == top;
}
//判满
bool Stack::full(){
    return MAX-1 == top;
}
//获取栈顶元素
datatype Stack::topdata(){
    cout<<"栈顶元素为:";
    return data[top];
}
//求栈的大小
int Stack::size(){
    cout<<this<<"栈的大小为:";
    return top+1;
}

main.cpp

#include"stack.h"

int main()
{
    Stack s;
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    s.push(5);
    s.push(6);
    cout<<s.size()<<endl;
    Stack s2=s;
    cout<<s2.size()<<endl;
    Stack s3=s2;
    cout<<s3.size()<<endl;
    s3.pop();
    s3.pop();
    s3.pop();
    cout<<s3.topdata()<<endl;
    s3.clear();
    s3.pop();
    cout<<s3.size()<<endl;
    cout<<s.size()<<endl;
    return 0;
}

效果图

封装循环队列:

queue.h

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
typedef int datatype;
#define MAX 5

using namespace std;

class Queue{
public:
    //构造函数
    Queue();
    //析构函数
    ~Queue();
    //拷贝构造函数
    Queue(const Queue &other);
    //入队
    int push(datatype e);
    //出队
    int pop();
    //清空队列
    void clear();
    //判空
    bool empty();
    //判满
    bool full();
    //求队列大小
    int size();

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

#endif // QUEUE_H

queue.cpp

#include"queue.h"

//构造函数
Queue::Queue():front(0),tail(0),data(new datatype[MAX])
{
    cout<<"构造函数"<<endl;
}
//析构函数
Queue::~Queue(){
    delete []data;
    cout<<"析构函数"<<endl;
}
//拷贝构造函数
Queue::Queue(const Queue &other):front(other.front),
    tail(other.tail),
    data(new datatype[MAX]){
    std::copy(other.data,other.data+MAX,data);
    cout<<"拷贝构造函数"<<endl;
}
//入队
int Queue::push(datatype e){
    if(Queue::full()){
        cout<<"队满,无法入队;"<<endl;
        return 0;
    }
    data[tail]=e;
    cout<<e<<"已入队"<<endl;
    tail=(tail+1)%MAX;

    return 1;
}
//出队
int Queue::pop(){
    if(Queue::empty()){
        cout<<"队空,无法出队;"<<endl;
        return 0;
    }
    cout<<data[front]<<"已出队"<<endl;
    front=(front+1)%MAX;
    return 1;
}
//清空队列
void Queue::clear(){
    cout<<"****清空队列****"<<endl;
    while(!Queue::empty()){
        Queue::pop();
    }
}
//判空
bool Queue::empty(){
    return front==tail;
}
//判满
bool Queue::full(){
    return (tail+1)%MAX==front;
}
//求队列大小
int Queue::size(){
    cout<<this<<"队列大小为:";
    return (tail-front+MAX)%MAX;
}

main.cpp

#include"queue.h"

int main()
{
    Queue q;
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(4);
    q.push(5);
    cout<<q.size()<<endl;
    Queue q2=q;
    cout<<q2.size()<<endl;
    Queue q3=q2;
    cout<<q2.size()<<endl;
    q2.clear();
    q2.pop();
    cout<<q2.size()<<endl;
    cout<<q.size()<<endl;

    return 0;
}

效果图

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值