day43:C++ day3 类、结构体与类的区别、this指针、类中特殊成员函数

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

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

stack.h:

#ifndef STACK_H
#define STACK_H

#include <iostream>
using namespace std;
#define MAX 50
class Stack
{
private:
    int *data;
    int top;
public:
    Stack():data(new int[MAX]),top(-1)
    {
        cout<<"Stack::无参构造函数"<<endl;
    }
    ~Stack()
    {
        cout<<"Stack::构析函数"<<endl;
    }
    Stack(const Stack &other)
    {
        this->data=new int[MAX];
        memcpy(this->data,other.data,sizeof(int)*MAX);
        this->top=other.top;
        cout<<"Stack::拷贝构造函数"<<endl;
    }
    void stack_push(Stack &S,int &&val);
    void stack_pop(Stack &S);
    void stack_clear(Stack &S);
    bool stack_empty(Stack &S);
    bool stack_full(Stack &S);
    int stack_gettop(Stack &S);
    int stack_len(Stack &S);
    void stack_show(Stack &S);
};
#endif // STACK_H

stack.cpp:

#include "stack.h"
void Stack::stack_push(Stack &S,int &&val)
{
    S.top++;
    S.data[top]=val;
}
void Stack::stack_pop(Stack &S)
{
    if(NULL==S.data || stack_empty(S))
    {
        cout<<"所给顺序栈不合法"<<endl;
    }
    cout<<S.data[top]<<"出栈成功"<<endl;
    S.top--;
}
void Stack::stack_clear(Stack &S)
{
    while(!stack_empty(S))
    {
        stack_pop(S);
    }
}
bool Stack::stack_empty(Stack &S)
{
    return -1==top;
}
bool Stack::stack_full(Stack &S)
{
    return MAX-1==S.top;
}
int Stack::stack_gettop(Stack &S)
{
    return S.data[top];
}
int Stack::stack_len(Stack &S)
{
    return top+1;
}
void Stack::stack_show(Stack &S)
{
    for(int i=top;i>=0;i--)
    {
        cout<<S.data[i]<<" ";
    }
    cout<<endl;
}

main.cpp:

#include"stack.h"

int main()
{
    Stack S;
    cout<<S.stack_empty(S)<<endl;
    S.stack_push(S,5);
    S.stack_push(S,8);
    S.stack_push(S,6);
    S.stack_push(S,7);
    S.stack_push(S,3);
    S.stack_push(S,9);
    cout<<S.stack_full(S)<<endl;
    cout<<"栈顶元素为"<<S.stack_gettop(S)<<endl;
    cout<<"栈的长度为"<<S.stack_len(S)<<endl;
    S.stack_show(S);
    S.stack_clear(S);
    S.stack_push(S,520);
    S.stack_show(S);
    return 0;
}

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

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

queue.h:

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>

#define MAX 50
using namespace std;
class Queue
{
private:
    int *data;
    int head;
    int tail;

public:
    Queue():data(new int[MAX]),head(0),tail(0)
    {
        cout<<"queue::无参构造函数"<<endl;
    }
    ~Queue()
    {
        delete []data;
        cout<<"queue::析构函数"<<endl;
    }
    Queue(const Queue &other):
        data(new int(*other.data)),
        head(other.head),
        tail(other.tail)
    {
        this->data=new int[MAX];
        memcpy(this->data,other.data,sizeof(int)*MAX);
        /*
        for(int i=other.head;i!=other.tail;i=(i+1)%MAX)
        {
            data[i]=other.data[i];
        }
        */
        this->head=other.head;
        this->tail=other.tail;
        cout<<"拷贝构造函数"<<endl;
    }
    void queue_push(Queue &Q,int &&val);
    void queue_pop(Queue &Q);
    void queue_clear(Queue &Q);
    bool queue_empty(Queue &Q);
    bool queue_full(Queue &Q);
    int queue_len(Queue &Q);
    void queue_show(Queue &Q);
};
#endif // QUEUE_H

queue.cpp:

#include "queue.h"
void Queue::queue_push(Queue &Q,int &&val)
{
    Q.data[tail]=val;
    tail=(tail+1)%MAX;
    return;
}
void Queue::queue_pop(Queue &Q)
{
    cout<<Q.data[head]<<"出队成功"<<endl;
    head=(head+1)%MAX;
    return;
}
void Queue::queue_clear(Queue &Q)
{
    while(!queue_empty(Q))
    {
        queue_pop(Q);
    }
}
bool Queue::queue_empty(Queue &Q)
{
    return Q.head==Q.tail;
}
bool Queue::queue_full(Queue &Q)
{
    return Q.head==(Q.tail+MAX)%MAX;
}
int Queue::queue_len(Queue &Q)
{
    return (Q.tail+MAX-Q.head)%MAX;
}
void Queue::queue_show(Queue &Q)
{
    for(int i=Q.head;i!=Q.tail;i=(i+1)%MAX)
    {
        cout<<Q.data[i]<<" ";
    }
    cout<<endl;
}

main.c

#include "queue.h"

int main()
{
    Queue Q;
    Q.queue_empty(Q);
    Q.queue_push(Q,5);
    Q.queue_push(Q,8);
    Q.queue_push(Q,5);
    Q.queue_push(Q,8);
    Q.queue_full(Q);
    Q.queue_pop(Q);
    Q.queue_show(Q);
    Q.queue_clear(Q);
    Queue L;
    L=Q;
    L.queue_show(L);
    return 0;
}

思维导图:有道笔记

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值