C++-day3作业

1、顺序栈类

头文件:

#ifndef STACK_H
#define STACK_H

#include <iostream>
#define MAX 40

using namespace std;

class Stack
{
private:
    int data[MAX];
    int top;
public:
    //定义无参构造函数
    Stack();

    //定义析构函数
    ~Stack();

    //定义拷贝构造函数
    Stack(const Stack &s);

    //判空
    bool stack_empty();

    //判满
    bool stack_full();

    //入栈
    int stack_push(int d);

    //出栈
    int stack_pop();

    //清空栈
    int pop_empty();

    //获取栈顶元素
    int stack_top();

    //求栈的大小
    int stack_size();
};


#endif // STACK_H

源文件:

#include"stack.h"

//定义无参构造函数
Stack::Stack():top(-1)
{
    cout<<"Stack::无参构造函数"<<endl;
}

//定义析构函数
Stack::~Stack()
{
    cout<<"Stack::析构函数:"<<this<<endl;
}

//定义拷贝构造函数
Stack::Stack(const Stack &s):top(s.top)
{
    for(int i=0;i<=s.top;i++)
    {
        data[i]=s.data[i];
    }
    cout<<"拷贝构造函数"<<endl;
}

//判空
bool Stack::stack_empty()
{
    if(top==-1)
    {
        return true;
    }
    return false;
}

//判满
bool Stack::stack_full()
{
    if(top==MAX)
    {
        return true;
    }
    return false;
}

//入栈
int Stack::stack_push(int d)
{
   if(Stack::stack_full())
   {
        cout<<"栈已满,入栈失败"<<endl;
        return 0;
   }
   top=top+1;
   data[top]=d;
   cout<<"入栈成功"<<endl;
   return 1;
}

//出栈
int Stack::stack_pop()
{
    if(Stack::stack_empty())
    {
        cout<<"栈已空,出栈失败"<<endl;
        return 0;
    }
    cout<<data[top]<<"出栈成功"<<endl;
    top=top-1;
    return 1;
}

//清空栈
int Stack::pop_empty()
{
    top=-1;
    return 1;
}

//获取栈顶元素
int Stack::stack_top()
{
    if(Stack::stack_empty())
    {
        cout<<"栈已空"<<endl;
        return 0;
    }
    cout<<"栈顶元素为:"<<data[top]<<endl;
    return 1;
}

//求栈的大小
int Stack::stack_size()
{
    cout<<"栈的大小为:"<<top+1<<endl;
    return 1;
}

测试文件:

#include"stack.h"

int main()
{
    Stack s;



    //调用入栈函数
    s.stack_push(9);
    s.stack_push(5);
    s.stack_push(2);
    s.stack_push(7);
    s.stack_push(6);
    s.stack_push(1);

    //调用求栈的大小函数
    s.stack_size();

    Stack s2=s;      //调用拷贝构造

    //调用出栈函数
    s.stack_pop();
    s.stack_pop();

    //调用获取栈顶元素函数
    s.stack_top();

    //调用清空栈函数
    s.pop_empty();

    return 0;
}

2、循环顺序队列类

头文件:

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#define MAX 40

using namespace std;

class Queue
{
private:
    int data[MAX];
    int front;       //记录队头元素的下标
    int tail;        //记录队尾元素的下标
public:
    //定义无参构造函数
    Queue();

    //定义析构函数
    ~Queue();

    //定义拷贝构造函数
    Queue(const Queue &s);

    //判空
    bool queue_empty();

    //判满
    bool queue_full();

    //入队
    int queue_push(int q);

    //出队
    int queue_pop();

    //清空队
    int pop_enpty();

    //求队列的大小
    int queue_size();


};

#endif // QUEUE_H

源文件:

#include"queue.h"

//定义无参构造函数
Queue::Queue():front(0),tail(0)
{
    cout<<"Queue::无参构造函数"<<endl;
}

//定义析构函数
Queue::~Queue()
{
   cout<<"Queue::析构函数:"<<this<<endl;
}

//定义拷贝构造函数
Queue::Queue(const Queue &q):front(q.front),tail(q.tail)
{
    for(int i=front;i<tail;i++)
    {
        data[i]=q.data[i];
    }
    cout<<"拷贝构造函数"<<endl;
}

//判空
bool Queue::queue_empty()
{
    if(front==tail)
    {
        return true;
    }
    return false;
}

//判满
bool Queue::queue_full()
{
    if((tail+1)%MAX==front)
    {
        return true;
    }
    return false;
}

//入队
int Queue::queue_push(int q)
{
    if(Queue::queue_full())
    {
        cout<<"队已满,入队失败"<<endl;
        return 0;
    }

    //将数据放入队尾所在地方
    data[tail]=q;

    //队尾后移
    tail=(tail+1)%MAX;
    cout<<"入队成功"<<endl;
    return 1;
}

//出队
int Queue::queue_pop()
{
    if(Queue::queue_empty())
    {
        cout<<"队已空,出队失败"<<endl;
        return 0;
    }

    //出队
    cout<<data[front]<<"出队成功"<<endl;

    //队头后移
    front=(front+1)%MAX;
    return 1;
}

//清空队
int Queue::pop_enpty()
{
    front=tail;
    return 1;
}

//求队列的大小
int Queue::queue_size()
{
    int s=(tail+MAX-front)%MAX;
    cout<<"队列的大小为"<<s<<endl;
    return 1;
}






测试文件:

#include"queue.h"

int main()
{
    Queue q;


    //调运入队函数
    q.queue_push(9);
    q.queue_push(5);
    q.queue_push(2);
    q.queue_push(7);
    q.queue_push(6);
    q.queue_push(1);
    q.queue_push(8);

    //调用求队列的大小
    q.queue_size();

    Queue q2=q;     //调用拷贝构造函数

    //调用出队函数
    q.queue_pop();
    q.queue_pop();

    //调用清空队
    q.pop_enpty();



    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值