2024.9.4

#include <iostream>
#include <cstring>
using namespace std;

template<typename T>
class Stack
{
private:
    int len;
    int count = 0;
    T *stack;
public:
    Stack():len(10)             //无参构造
    {
        stack = new T[len];
        stack[len] = {0};
    }
    Stack(int len):len(len)         //有参构造
    {
        stack = new T[len];
        stack[len] = {0};
    }
    Stack(Stack &other):len(other.len)  //拷贝构造函数
    {
        stack = new T[len];
        for(int i=0;i<len;i++)
        {
            stack[i] = other.stack[i];
        }
    }
    Stack &operator=(const Stack &other)    //拷贝赋值函数
    {
        if(this != &other)
        {
            this->count = other.count;
            this->len = other.len;
            int *newstack = new T[this->len];
            for(int i=0;i<len;i++)
            {
                newstack[i] = other.stack[i];
            }
            delete [] stack;
            stack = newstack;

        }
        return *this;
    }
    ~Stack()        //析构函数
    {
        delete [] stack;
    }
    T top();
    bool empty();
    int size();
    int push(T n);
    void expand();
    void show();
    int pop();
};
template<typename T>
T Stack<T>::top()
{
    if(empty())
    {
        cout<<"error" <<endl;
        return -1;
    }
    return stack[count-1];
}
template<typename T>
bool Stack<T>::empty()
{
    return count == 0;
}

template<typename T>
int Stack<T>::size()
{
    return count;
}

template<typename T>
int Stack<T>::push(T n)
{
    if(count == len)
    {
        expand();
    }
    stack[count++] = n;
    return 0;
}

template<typename T>
void Stack<T>::expand()
{
    len = len * 2;
    int *newstack = new T[len];
    for(int i=0;i<count;i++)
    {
        newstack[i] = stack[i];
    }
    delete [] stack;
    stack = newstack;
}
template<typename T>
void Stack<T>::show()
{
    for(int i=count-1;i>=0;i--)
    {
        cout<<stack[i]<<" ";
    }
    cout<<endl;
}

template<typename T>
int Stack<T>::pop()
{
    if(empty())
    {
        cout<<"error"<<endl;
        return -1;
    }
    stack[--count] = 0;
    return 0;
}
template<typename K>
class Queue
{
private:
    int len;
    int count = 0;
    K *queue;
public:
    Queue():len(10)
    {
        queue = new K[len];
        queue[len] = {0};
    };
    Queue(int n):len(n)
    {
        queue = new K[len];
        queue[len] = {0};
    };
    Queue(Queue &other):len(other.len)
    {
        queue = new K[len];
        for(int i=0;i<len;i++)
        {
            queue[i] = other.queue[i];
        }
    };
    Queue &operator=(const Queue &other)    //拷贝赋值函数
    {
        if(this != &other)
        {
            this->count = other.count;
            this->len = other.len;
            int *newqueue = new K[this->len];
            for(int i=0;i<len;i++)
            {
                newqueue[i] = other.queue[i];
            }
            delete [] queue;
            queue = newqueue;

        }
        return *this;
    }
    ~Queue()
    {
        delete []queue;
    };
    K front();
    K back();
    bool empty();
    int size();
    int push(K n);
    void pop();
    void expand();
    void show();
};
template<typename K>
K Queue<K>::front()
{
    if(empty())
    {
        cout<<"error"<<endl;
        return  -1;
    }
    return queue[count-1];
}
template<typename K>
K Queue<K>::back()
{
    if(empty())
    {
        cout<<"error"<<endl;
        return  -1;
    }
    return queue[0];
}
template<typename K>
bool Queue<K>::empty()
{
    return count == 0;
}
template<typename K>
int Queue<K>::size()
{
    return count;
}
template<typename K>
int Queue<K>::push(K n)
{
    if(count == len)
    {
        expand();
    }
    queue[count++] = n;
    return 0;
}
template<typename K>
void Queue<K>::pop()
{
    for(int i=0;i<count;i++)
    {
        queue[i] = queue[i+1];
    }
    count--;
}
template<typename K>
void Queue<K>::expand()
{
    len = len * 2;
    int *newqueue = new K[len];
    for(int i=0;i<count;i++)
    {
        newqueue[i] = queue[i];
    }
    delete [] queue;
    queue = newqueue;
}
template<typename K>
void Queue<K>::show()
{
    for(int i=0;i<count;i++)
    {
        cout<<queue[i]<<"  ";
    }
    cout<<endl;
}
int main()
{
    Stack<int> s1(3);
    s1.push(1);
    s1.push(2);
    s1.push(3);
    s1.push(3);
    s1.pop();
    Stack<int> s2;
    s2 = s1;
    s1.show();
    s2.show();
    cout<<s2.top()<<"   "<<s2.size()<<endl;
    cout<<"************"<<endl;
    Queue<int> q1(3);
    q1.push(1);
    q1.push(2);
    q1.push(3);
    q1.show();
    q1.pop();
    q1.show();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值