C++DAY6

模板类封装顺序栈

#include <iostream>

#define MAXSIZE 10

using namespace std;

template <typename T>
class Stack
{
private:
    T *ptr;
    T top;
public:
    //无参构造
    Stack<T>():ptr(new T[10]),top(-1) {cout << "无参构造" << endl;}
    //析构函数
    ~Stack();
    //拷贝赋值函数
    Stack &operator = (const Stack &other);
    //判空函数
    bool stack_empty();
    //判满函数
    bool stack_full();
    //入栈函数
    T push(T data);
    //出栈函数
    T pop();
    //遍历栈函数
    void output();
    //返回栈顶元素的引用
    T &stack_top();
    //返回元素个数
    T size();
};

template <typename T>
Stack<T>::~Stack()
{
    delete [] ptr;
    ptr = nullptr;
    cout << "构析函数完成" << endl;
}

template<typename T>
Stack<T> &Stack<T>::operator =(const Stack &other)
{
    if(this != &other)
    {
        top = other.top;
        delete []ptr;
        ptr = new T[MAXSIZE];
        for (int i = 0;i <= top;i++)
            ptr[i] = other.ptr[i];
    }
}

template <typename T>
bool Stack<T>::stack_empty()
{
    return (-1 == top);
}

template <typename T>
bool Stack<T>::stack_full()
{
    return (MAXSIZE-1 == top);
}

template <typename T>
T Stack<T>::push(T data)
{
    if(stack_full())
    {
        cout << "栈满" << endl;
        return -1;
    }
    ptr[top+1] = data;
    top++;
    return 0;
}

template <typename T>
T Stack<T>::pop()
{
    if(stack_empty())
    {
        cout << "栈空" << endl;
        return -1;
    }

    return ptr[top--];
}

template <typename T>
void Stack<T>::output()
{
    if(stack_empty())
    {
        cout << "栈空" << endl;
        return;
    }
    for(int i = 0;i <= top;i++)
    {
        cout << ptr[i] << " ";
    }
    cout << endl;
}

template <typename T>
T &Stack<T>::stack_top()
{
    return ptr[top];
}

template<typename T>
T Stack<T>::size()
{
    return top+1;
}

int main()
{
    Stack<int> s;
    int data;
    char ch;
    do
    {
        int data = 0;
        cout << "请输入你要入栈的元素:";
        cin >> data;
        while(getchar()!=10);
        s.push(data);
        cout << "你还想要继续入栈吗?y/n" << endl;
        cin >> ch;
        while(getchar()!=10);
    }while('y' == ch);

    s.output();

    do
    {
        data = s.pop();
        cout << "出栈的元素是:" << data << endl;
        cout << "你想要出栈吗?y/n" << endl;
        cin >> ch;
        while(getchar()!=10);
    }while('y' == ch);

    s.output();

    cout  << "栈顶元素的引用: " << s.stack_top()<< endl;

    return 0;
}


模板类封装循环顺序队列

 

#include <iostream>

using namespace std;

int error = -1;
template <typename T>
class Queue
{
private:
    T *ptr;
    T front;
    T rear;
    T maxsize;
public:
    //无参构造
    Queue():ptr(new T[10]),front(0),rear(0),maxsize(10) {cout << "无参构造" << endl;}
    //析构函数
    ~Queue();
    //判空函数
    bool queue_empty();
    //判满函数
    bool queue_full();
    //入队函数
    T enqueue(T data);
    //出队函数
    T dequeue();
    //遍历函数
    void output();
    //获取队头数据
    const T &getfront();
    //获取队尾数据
    const T &getrear();
};

template <typename T>
Queue<T>::~Queue()
{
    delete [] ptr;
    ptr = nullptr;
    cout << "构析函数完成" << endl;
}

template <typename T>
bool Queue<T>::queue_empty()
{
    return (front == rear);
}

template <typename T>
bool Queue<T>::queue_full()
{
    return (front == (rear+1)%maxsize);
}

template <typename T>
T Queue<T>::enqueue(T data)
{
    if(queue_full())
    {
        cout << "队满" << endl;
        return -1;
    }
    ptr[rear] = data;
    rear = (rear+1)%maxsize;
    return 0;
}

template <typename T>
T Queue<T>::dequeue()
{
    if(queue_empty())
    {
        cout << "队空" << endl;
        return -1;
    }
    T e;
    e= ptr[front];
    front = (front+1)%maxsize;
    return e;
}

template <typename T>
void Queue<T>::output()
{
    if(queue_empty())
    {
        cout << "队空" << endl;
        return;
    }
    for(int i = front;i!=rear;i=(i+1)%maxsize)
    {
        cout << ptr[i] << " ";
    }
    cout << endl;
}

template<typename T>
const T &Queue<T>::getfront()
{
    if(queue_empty())
    {
        cout << "队空" << endl;
        return error;
    }
    return ptr[front];
}

template<typename T>
const T &Queue<T>::getrear()
{
    if(queue_empty())
    {
        cout << "队空" << endl;
        return error;
    }
    return ptr[(rear+maxsize-1)%maxsize];
}

int main()
{
    Queue<int> q;
    int data;
    char ch;
    do
    {
        int data = 0;
        cout << "请输入你要入队的元素:";
        cin >> data;
        while(getchar()!=10);
        q.enqueue(data);
        cout << "你还想要继续入队吗?y/n" << endl;
        cin >> ch;
        while(getchar()!=10);
    }while('y' == ch);

    q.output();

    do
    {
        data = q.dequeue();
        cout << "出队的元素是:" << data << endl;
        cout << "你想要出队吗?y/n" << endl;
        cin >> ch;
        while(getchar()!=10);
    }while('y' == ch);

    q.output();

    cout  << "队头元素的引用: " << q.getfront()<< endl;
    cout  << "队尾元素的引用: " << q.getrear()<< endl;

    return 0;
}

思维导图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值