7_22作业

思维导图
在这里插入图片描述
在这里插入图片描述
顺序栈

#include <iostream>

using namespace std;

class My_stack
{
private:
    int *ptr;
    int top;
    int length;
public:
    My_stack():ptr(new int[10]),top(-1),length(10){cout<<"无参创建"<<endl;}
    My_stack(int n):ptr(new int[n]),top(-1),length(n){cout<<"有参创建"<<endl;}
    ~My_stack()
    {

        cout<<"stack 析构"<<endl;
        delete []ptr;
    }
    int isempty();
    int isfull();
    void input(int &data);
    void output();
    void erg();
    int &gettop()
    {
        return ptr[top];
    }


};



void My_stack::erg()
{
    for(int i=top;i>=0;i--)
    {
        cout<<ptr[i]<<endl;
    }

}

void My_stack::input(int &data)
{
    ptr[++top]=data;

}

void My_stack::output()
{
    cout<<ptr[top--]<<endl;
}

int My_stack::isempty()
{
    if(top==-1)
    {
        cout<<"栈为空"<<endl;
        return 0;
    }
    else
    {
        cout<<"栈不为空"<<endl;
        return 1;
    }
}

int My_stack::isfull()
{
    if(top==length-1)
    {
        cout<<"栈满"<<endl;
        return 0;
    }
    else
    {
        cout<<"栈未满"<<endl;
        return  1;
    }
}


int main()
{
    int len;
    cout<<"请输入栈的长度"<<endl;
    cin>>len;

    My_stack m1(len);

    string s;
    while(1)
    {
        cout<<"输入指令"<<endl;

        cin>>s;
        if(s=="input")
        {
            char s1;
            cout<<"是否进栈"<<endl;
            cin>>s1;
            while(s1=='y'&&m1.isfull())
            {
                int temp;
                cout<<"输入进栈值"<<endl;
                cin>>temp;
                m1.input(temp);
                cout<<"是否继续进栈"<<endl;
                cin>>s1;

            }


        }
        else if(s=="output")
        {
            char s1;
            cout<<"是否出栈"<<endl;
            cin>>s1;

            while(s1=='y'&&m1.isempty())
            {
                m1.output();
                cout<<"是否继续出栈"<<endl;
                cin>>s1;

            }

        }
        else if(s=="ergodic")
        {
            m1.erg();

        }
        else if(s=="gettop")
        {
            cout<<m1.gettop()<<endl;
        }
        else if(s=="exit")
        {
            break;
        }

    }
    m1.~My_stack();

    return 0;
}

循环队列



#include <iostream>

using namespace std;
template <typename T>
class Cricle
{
private:
    T *ptr;  //首地址
    int size;  //长度
    int front;  //队头
    int rear;   //队尾

public:
    Cricle(){};
    Cricle(int s):ptr(new T[size]),size(s),front(0),rear(0){};
    ~Cricle()
    {
        delete []ptr;
    };

    Cricle &operator = (const Cricle &other)
    {
        if(this == &other)
        {
            return *this;
        }
        delete []ptr;

        return strncat(this->ptr,&other.ptr,size);
    }

    bool emtpy()
    {
        return front == rear;
    }

    bool full()
    {
        return front = (rear+1)%size;
    }

    int setsize()
       {
           return size = front;
       }


    int push(T data)
    {
        if(full())
        {
            return -1;
        }
        rear = (rear +1)%size;
        return 0;
    }

    int pop()
    {
        if(emtpy())
        {
            return -1;
        }
        front = (front +1)%size;
        return 0;
    }

    int getsize()
    {
        return (size - front+rear)%size;
    }

    void show()
    {
        if(emtpy())
        {
            return ;
        }
        for(int i=front;i!=rear;i=(i+1)%size)
        {
            cout<<ptr[i]<<endl;
        }
    }

};

int main()
{

    return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值