C++ 第六讲 模板类实现顺序栈 模板类实现顺序栈

思维导图

 

模板类实现顺序栈

主函数

#include "test.h"

int main()
{
    Stack <DATATYPE>s; // <int>/<string>/<char>

    char ch = 0;
    DATATYPE e = 0; // int / string / char
    //入栈
    do{
        if(true == s.my_full()){
            break ;
        }
        cout << "入栈,请输入要入栈的数据 >>>" << endl;
        cin >> e;
        s.my_push (e);
        cout << "是否继续入栈 Y/N" <<endl;
        cin >> ch ;
    }while('Y' == ch || 'y' == ch);
    s.my_top();
    s.my_size();
    s.show();

    //出栈
    do{
        cout << "出栈的数据是 >>>" << s.my_pop()<< endl;
        if(true == s.my_empty()){
            break;
        }
        cout << "是否继续出栈 Y/N" <<endl;
        cin >> ch ;

    }while('Y' == ch || 'y' == ch);  
    s.my_top();
    s.my_size();
    s.show();

    Stack <DATATYPE>s1;
    s1 = s;
    s1.show();


    return 0;
}

头文件

#ifndef TEST_H
#define TEST_H

#include <iostream>
#define MAXSIZE 10      //栈 大小
#define DATATYPE char  //数据类型

using namespace std;

template<class T>
class Stack
{
protected:
    int top;
    T *ptr;
public:
    //无参构造
    Stack():ptr(new T [MAXSIZE-1]),top(-1) {
        cout << "创建栈" << endl;
    }
    //拷贝赋值函数
    Stack &operator =(const Stack &other);
    //访问栈顶元素
    T &my_top();
    //判空
    bool my_empty();
    //判满
    bool my_full();
    //返回容纳的元素个数
    int my_size();
    //入栈
    //void my_push( char &data);
    void my_push( T &data);
    //出栈
    T my_pop();
    //输出
    void show(){
        int i = 0;
        for(; i <= top; i++){
            cout << ptr[i] << "  ";
        }
        cout << endl;
    }


};

//拷贝赋值函数
template<class T>
Stack<T> &Stack<T>::operator =(const Stack &other){
    cout << "拷贝赋值函数" << endl;
    this->top = other.top;
    delete []ptr;
    this->ptr = new T ;
    int i = 0;
    for( ;i <= top; i++){
        ptr[i] = other.ptr[i];
    }
    return *this;
}

//判空
template<class T>
bool Stack<T>::my_empty(){
    if(top == -1){
        cout << ">>> 栈空 <<<" << endl;
        return true;
    }else{
        return false;
    }
}

//判满
template<class T>
bool Stack<T>::my_full(){
    if(MAXSIZE -1 == top){
        cout << ">>> 栈满 <<<" << endl;
        return true;
    }
    return false;
}
//返回容纳的元素个数
template<class T>
int Stack<T>::my_size(){
    cout << "元素个数 >>> " << top+1 <<endl;
    return top+1;
}

//入栈
template<class T>
void Stack<T>::my_push(T &data){
    ptr[++top] = data;
}

//出栈
template<class T>
T Stack<T>::my_pop(){
    return ptr[top--];
}

//访问栈顶元素
template<class T>
T &Stack<T>::my_top(){
    cout<< "栈顶元素 >>> " << ptr[top]<< endl;
    return ptr[top];
}
#endif // TEST_H

模板类实现循环队列


#include <iostream>

using namespace std;
#define MAXSIZE 10
#define DATATYPE int

template<class T>
class Queue
{
protected:
    int rear;
    int front;
    T *data;

public:
    //无参构造
    Queue();
    //析构函数
    ~Queue();
    //拷贝赋值函数
    Queue &operator=(const Queue &other);
    //访问第一个元素
    T my_front();
    //访问最后一个元素
    T my_rear();
    //判空
    bool my_empty();
    //判满
    bool my_full();
    //插入
    void my_push(T &e);
    //删除
    T my_pop();
    //元素个数
    int my_size();
    //输出
    void show();

};


int main()
{
    Queue <DATATYPE>q;
    //入队
    DATATYPE e;
    char ch = 0;
    do{
        if(true == q.my_full()){
            break ;
        }
        cout << "入队,请输入要入队的数据 >>>  " ;
        cin >> e;
        q.my_push (e);
        cout << "是否继续入队 Y/N >>>  ";
        cin >> ch ;
    }while('Y' == ch || 'y' == ch);
    q.my_size();
    q.show();

    //出栈
    do{
        cout << "出队的数据是 >>>  " << q.my_pop() << endl ;
        if(true == q.my_empty()){
            break;
        }
        cout << "是否继续出队 Y/N >>>  " ;
        cin >> ch ;

    }while('Y' == ch || 'y' == ch);
    q.my_size();
    q.show();

    Queue <DATATYPE>q1;
    q1 = q;
    q1.show();
    q1.my_size();
    q1.my_front();
    q1.my_rear();

    return 0;
}

template<class T>
Queue<T>::Queue():
    rear(0),
    front(0),
    data(new T[MAXSIZE]) {
    cout << "无参构造" <<endl;
}

template<class T>
Queue<T>::~Queue(){
    delete [] data;
    cout << "析构函数" <<endl;
}
//拷贝赋值函数
template<class T>
Queue<T> &Queue<T>::operator=(const Queue &other){
    cout << "拷贝赋值" << endl;
    if(this != &other){
        delete [] data;
        this->data = new T[MAXSIZE]{*(other.data)};
        this->rear = other.rear;
        this->front = other.front;
        int i = 0;
        for(i = front; i!= rear; i=(i+1)%MAXSIZE){
            data[i] = other.data[i];
        }
    }
    return *this;
}

//访问第一个元素
template<class T>
T Queue<T>::my_front(){
    cout << "第一个元素为 >>>  " << data[front] << endl;
    return data[front];
}
//访问最后一个元素
template<class T>
T Queue<T>::my_rear(){
    cout << "最后一个元素为 >>>  " << data[rear-1] << endl;
    return data[rear-1];
}

//判空
template<class T>
bool Queue<T>::my_empty(){
    if(rear == front){
        cout << "队空" << endl;
        return true;
    }
    return false;
}
//判满
template<class T>
bool Queue<T>::my_full(){
    if(front == (rear+1)%MAXSIZE){
        cout << "队满" << endl;
        return true;
    }
    return false;
}

//尾插
template<class T>
void Queue<T>::my_push(T &e){
    data[rear++] = e;
}
//头删
template<class T>
T Queue<T>::my_pop(){
    return data[front++];
}

//元素个数
template<class T>
int Queue<T>::my_size(){
    return (MAXSIZE - front + rear)%MAXSIZE;
}

//输出
template<class T>
void Queue<T>::show(){
    int i = 0;
    for(i = front; i!= rear; i=(i+1)%MAXSIZE){
        cout<< data[i] << "  " ;
    }
    cout << endl;
}

猜你喜欢

转载自blog.csdn.net/MaGuangming001/article/details/131871462