用模板完成顺序栈和顺序队列

顺序栈 

#include <iostream>
using namespace std;
#define MAX 50
 
template<typename T>
class Stack
{
private:
    T *data;
    int top;
public:
    Stack():data(new T[MAX]),top(-1)
    {
        cout<<"Stack::无参构造函数"<<endl;
    }
    ~Stack()
    {
        delete []data;
        cout<<"Stack::构析函数"<<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<<"Stack::拷贝构造函数"<<endl;
    }
    void stack_push(T &&val);
    void stack_pop();
    void stack_clear();
    bool stack_empty();
    bool stack_full();
    T stack_gettop();
    int stack_len();
    void stack_show();
};
template<typename T>
void Stack<T>::stack_push(T &&val)
{
    this->top++;
    this->data[top]=val;
}
 
template<typename T>
void Stack<T>::stack_pop()
{
    if(NULL==this->data || stack_empty())
    {
        cout<<"所给顺序栈不合法"<<endl;
    }
    cout<<this->data[top]<<"出栈成功"<<endl;
    this->top--;
}
 
template<typename T>
void Stack<T>::stack_clear()
{
    this->top=-1;
}
 
template<typename T>
bool Stack<T>::stack_empty()
{
    return -1==this->top;
}
 
template<typename T>
bool Stack<T>::stack_full()
{
    return MAX-1==this->top;
}
 
template<typename T>
T Stack<T>::stack_gettop()
{
    return this->data[top];
}
 
template<typename T>
int Stack<T>::stack_len()
{
    return this->top+1;
}
 
template<typename T>
void Stack<T>::stack_show()
{
    for(int i=top;i>=0;i--)
    {
        cout<<this->data[i]<<" ";
    }
    cout<<endl;
}

顺序队列


#include <iostream>
 
#define MAX 50
using namespace std;
 
template<typename T>
class Queue
{
private:
    T *data;
    T head;
    T tail;
 
public:
    Queue():data(new T[MAX]),head((T)0),tail((T)0)
    {
        cout<<"queue::无参构造函数"<<endl;
    }
    ~Queue()
    {
        delete []data;
        cout<<"queue::析构函数"<<endl;
    }
    Queue(const Queue &other):
        data(new T(*other.data)),
        head(other.head),
        tail(other.tail)
    {
        this->data=new T[MAX];
        memcpy(this->data,other.data,sizeof(T)*MAX);
        this->head=other.head;
        this->tail=other.tail;
        cout<<"拷贝构造函数"<<endl;
    }
    void queue_push(T &&val);
    void queue_pop();
    void queue_clear();
    bool queue_empty();
    bool queue_full();
    int queue_len();
    void queue_show();
};
template<typename T>
void Queue<T>::queue_push(T &&val)
{
    data[tail]=val;
    tail=(tail+1)%MAX;
    return;
}
template<typename T>
void Queue<T>::queue_pop()
{
    cout<<data[head]<<"出队成功"<<endl;
    head=(head+1)%MAX;
    return;
}
template<typename T>
void Queue<T>::queue_clear()
{
    while(!queue_empty())
    {
        queue_pop();
    }
}
template<typename T>
bool Queue<T>::queue_empty()
{
    return head==tail;
}
template<typename T>
bool Queue<T>::queue_full()
{
    return head==(tail+MAX)%MAX;
}
template<typename T>
int Queue<T>::queue_len()
{
    return (tail+MAX-head)%MAX;
}
template<typename T>
void Queue<T>::queue_show()
{
    for(int i=head;i!=tail;i=(i+1)%MAX)
    {
        cout<<data[i]<<" ";
    }
    cout<<endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值