C++ 第六讲

思维导图

 

 

模板类实现顺序栈

主函数

#include "test.h"

int main()
{
    Stack s; // //

    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" <> 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" <> ch ;

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

    Stack s1;
    s1 = s;
    s1.show();


    return 0;
}

头文件

#ifndef TEST_H
#define TEST_H

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

using namespace std;

template
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
Stack &Stack::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
bool Stack::my_empty(){
    if(top == -1){
        cout << ">>> 栈空 <<<" << endl;
        return true;
    }else{
        return false;
    }
}

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

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

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

模板类实现循环队列


#include 

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

template
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 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 q1;
    q1 = q;
    q1.show();
    q1.my_size();
    q1.my_front();
    q1.my_rear();

    return 0;
}

template
Queue::Queue():
    rear(0),
    front(0),
    data(new T[MAXSIZE]) {
    cout << "无参构造" <
Queue::~Queue(){
    delete [] data;
    cout << "析构函数" <
Queue &Queue::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
T Queue::my_front(){
    cout << "第一个元素为 >>>  " << data[front] << endl;
    return data[front];
}
//访问最后一个元素
template
T Queue::my_rear(){
    cout << "最后一个元素为 >>>  " << data[rear-1] << endl;
    return data[rear-1];
}

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

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

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

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

你可能感兴趣的:(C++,c++,算法,开发语言,c语言,流程图)