c++day3

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

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

#include <iostream>

using namespace std;
class Stack
{
private:
    int data[50];
    int top;//记录栈顶的变量
public:
    //构造函数
    Stack():top(-1)//栈顶初始化为-1
    {
        cout<<"构造函数"<<endl;

    }
    //判断栈是否为满
    bool S_full()
    {
        return  top==49;
    }
    //判断栈是否为空
    bool S_empty()
    {
        return  top==-1;
    }
    //入栈
    void S_push(const int&item)
    {
        if(S_full())
        {
            cout<<"栈已满"<<endl;
        }
        else
        {
            data[++top]=item;//入栈操作
        }
    }
    //出栈
    int S_pop()
    {
        if(S_empty())
        {
            cout<<"栈空的"<<endl;
            return -1;
        }
        else
        {
            return data[top--];//出栈操作
        }
    }
    //查看栈顶元素
    void S_peek()
    {
        if(S_empty())
        {
            cout<<"栈空的"<<endl;

        }
        else
        {
            cout<<"栈顶元素为:"<<data[top]<<endl;
        }
    }
    //求栈的大小
    void S_size()
    {
        cout<<"栈的大小:"<<top+1<<endl;
    }
    //清空栈
    void S_free()
    {
        while(1)
        {
        if(S_empty())
        {
          cout<<"栈已清空"<<endl;
          break;
        }
        else
        {
           S_pop();
        }
    }
    }


    //析构函数
    ~Stack()
    {
        cout<<"析构函数"<<endl;
    }
    //拷贝构造函数
    Stack(const Stack& o) {
        cout<<"拷贝构造函数被调用了"<<endl;
        for(int i=0;i<=top;i++)
        {
            data[i]=o.data[i];
        }
        top=o.top;

    }
    //遍历栈
    void S_show()
    {
        cout<<"栈的元素:";
        for(int i=top;i>=0;i--)
        {
            cout<<data[i]<<" ";
        }
        cout<<endl;
    }

};
int main()
{

    Stack  s;
    s.S_push(1);
    s.S_push(2);
    s.S_push(3);
    s.S_push(4);
    s.S_pop();
    s.S_push(5);
    s.S_push(6);
    s.S_peek();
    s.S_size();
    s.S_free();
    s.S_size();
    s.S_push(5);
    s.S_push(6);
    s.S_size();
    s.S_peek();
    s.S_show();



    return 0;
}

 

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

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

#include <iostream>

using namespace std;
class Queue
{
private:
    int data[10];
    int front;//记录头元素位置
    int tail;//记录尾元素位置
public:
    //构造函数
    Queue():front(0),tail(0)//初始化
    {
        cout<<"构造函数"<<endl;

    }
    //判断队列是否为满
    bool S_full()
    {
        return  (tail+1)%10==front;
    }
    //判断队列是否为空
    bool S_empty()
    {
        return  front==tail;
    }
    //入队
    void S_push(const int&item)
    {
        if(S_full())
        {
            cout<<"队列已满"<<endl;
        }
        else
        {
            data[tail]=item;//入队操作
            //队尾后移动
            tail=(tail+1)%10;
            cout<<"入队成功"<<endl;
        }
    }
    //出队
    void S_pop()
    {
        if(S_empty())
        {
            cout<<"队空的"<<endl;

        }
        else
        {
            cout<<data[front]<<"出队成功"<<endl;//出队操作
            //队头后移
            front=(front+1)%10;

        }
    }

    //求队列的大小
    void S_size()
    {
        cout<<"队列的大小:"<<(tail+10-front)%10<<endl;
    }
    //清空队列
    void S_free()
    {
        while(1)
        {
        if(S_empty())
        {
          cout<<"队列已清空"<<endl;
          break;
        }
        else
        {
           S_pop();
        }
    }
    }


    //析构函数
    ~Queue()
    {
        cout<<"析构函数"<<endl;
    }
    //拷贝构造函数
    Queue(const Queue& o) {
        cout<<"拷贝构造函数被调用了"<<endl;
        for(int i=front;i<=tail;i=(i+1)%10)
        {
            data[i]=o.data[i];
        }
       front=o.front;
       tail=o.tail;

    }
    //遍历队列
    void S_show()
    {
        cout<<"队列的元素:";
        for(int i=front;i!=tail;i=(i+1)%10)
        {
            cout<<data[i]<<" ";
        }
        cout<<endl;
    }

};
int main()
{

    Queue  s;
    s.S_push(1);
    s.S_push(2);
    s.S_push(3);
    s.S_push(4);
    s.S_pop();
    s.S_push(5);
    s.S_push(6);
    s.S_size();
    s.S_free();
    s.S_size();
    s.S_push(5);
    s.S_push(6);
    s.S_push(1);
    s.S_push(3);
    s.S_push(1);
    s.S_push(4);
    s.S_size();
    s.S_show();




    return 0;
}

 思维导图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值