C++作业

顺序栈

#include <iostream>
#define MAX 10

using namespace std;

template<typename T>
class Stack
{
private:
    T *data;
    int top;
public:
    Stack():top(-1)
    {
        data=new T[MAX];
        cout<<"无参构造"<<endl;
    }

    //拷贝构造函数
    Stack(const Stack<T> &other)
    {
        if(data!=NULL)
        {
            delete []data;
        }
        this->data=new T[MAX];
        memcpy(this->data,other.data,sizeof(T)*MAX);
        this->top=other.top;
        cout<<"拷贝构造函数"<<endl;
    }

    //定义析构函数
    ~Stack()
    {
        delete []data;
        cout<<"析构函数"<<endl;
    }

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

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

    //入栈
    int stack_push(T d)
    {
       if(Stack<T>::stack_full())
       {
           cout<<"栈已满,入栈失败"<<endl;
           return 0;
       }

       top=top+1;
       data[top]=d;
       cout<<"入栈成功"<<endl;
       return 1;
    }

    //出栈
    int stack_pop()
    {
        if(Stack<T>::stack_empty())
        {
            cout<<"栈已空,出栈失败"<<endl;
            return 0;
        }

        cout<<data[top]<<"出栈成功"<<endl;
        top=top-1;
        return 1;
    }

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

    //获取栈顶元素
    int stack_top()
    {
        if(Stack<T>::stack_empty())
        {
            cout<<"栈已空,获取失败"<<endl;
            return 0;
        }

        cout<<data[top]<<endl;
        return 1;
    }

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


int main()
{
    //调用无参构造
    Stack<int> s1;
    Stack<string> s2;

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

    s2.stack_push("hello");
    s2.stack_push(" * ");
    s2.stack_push("world");

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

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

    //调用拷贝构造函数
    Stack<int>s3=s1;

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

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

    return 0;
}

顺序循环队列

#include <iostream>
#define MAX 10

using namespace std;

template<typename T>
class Queue
{
private:
    T *data;
    int front;
    int tail;
public:
    //无参构造
    Queue():front(0),tail(0)
    {
        data=new T[MAX];
        cout<<"无参构造"<<endl;
    }

    //拷贝构造函数
    Queue(const Queue &other):front(other.front),tail(other.tail)
    {
        if(data!=NULL)
        {
            delete[]data;
        }
        this->data=new T[MAX];
        memcpy(this->data,other.data,sizeof(T)*MAX);
        cout<<"这是拷贝构造函数"<<endl;
    }

    //定义析构函数
    ~Queue()
    {
        delete []data;
        cout<<"析构函数"<<endl;
    }

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

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

    //入队
    int Queue_push(T d)
    {
        if(Queue_full())
        {
            cout<<"队已满,入队失败"<<endl;
            return 0;
        }
        data[tail]=d;
        tail=(tail+1)%MAX;
        cout<<"入队成功"<<endl;
        return 1;
    }

    //出队
    int Queue_pop()
    {
       if(Queue_empty())
       {
           cout<<"队已空,出队失败"<<endl;
           return 0;
       }
       cout<<data[front]<<"出队成功"<<endl;
       return 1;
    }

    //清空队
    int pop_empty()
    {
        front=tail;
        return 0;
    }

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


int main()
{
    //调用无参构造
    Queue<int> q1;
    Queue<string> q2;

    //调用入队函数
    q1.Queue_push(9);
    q1.Queue_push(5);
    q1.Queue_push(2);
    q1.Queue_push(7);
    q1.Queue_push(6);

    q2.Queue_push("hello");
    q2.Queue_push(" * ");
    q2.Queue_push("world");

    //调用求队的大小函数
    q1.Queue_size();
    q2.Queue_size();

    //调用拷贝构造函数
    Queue<int>q3=q1;

    //调用出队函数
    q1.Queue_pop();
    q1.Queue_pop();
    q2.Queue_pop();

    //调用清空队函数
    q1.pop_empty();
    q2.pop_empty();


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值