【C++】day6

顺序栈:

#include 

using namespace std;

template <class T>
class SeqStack
{
private:
    T* data;  //存储栈元素的数组
    int top;  //栈顶指针
    int maxSize;  //最大容量

public:
    //构造函数
    SeqStack(int size){
        data = new T[size];
        top = -1;
        maxSize = size;
    }

    //析构函数
    ~SeqStack(){
        delete[] data;
    }

    //判空
    bool my_empty(){
        return top == -1;
    }

    //判满
    bool my_full(){
        return top == maxSize - 1;
    }

    //入栈
    void push(T x){
        if (my_full()){
            cout << "栈满!" << endl;
            return;
        }
        data[++top] = x;
        cout << "入栈成功!" << endl;
    }

    //出栈
    void pop(){
        if (my_empty()){
            cout << "栈空!" << endl;
            return;
        }
        top--;
        cout << "出栈成功!" << endl;
    }

    //获取栈顶元素
    T getTop(){
        if (my_empty()){
            cout << "栈空!" << endl;
            return NULL;
        }
        return data[top];
    }
};

int main()
{
    //创建顺序栈
    SeqStack<int> s(5);

    //入栈
    int i = 5;
    int data = 0;
    while(i){
        cout << "请输入一个栈元素:" << endl;
        cin >> data;
        s.push(data);
        i--;
    }

    //调用判满函数
    if(s.my_full()){
        cout << "栈满!" << endl;
    }else{
        cout << "没有栈满!" << endl;
    }

    //出栈
    s.pop();
    s.pop();

    //获取栈顶元素
    cout << "topdata = " << s.getTop() << endl;

    //调用判空函数
    if(s.my_empty()){
        cout << "栈空!" << endl;
    }else{
        cout << "没有栈空!" << endl;
    }

    return 0;
}

循环顺序队列:

#include 

using namespace std;

template <class T>
class Queue
{
private:
    T* queue;   //队列数组指针
    int front;  //队头指针
    int rear;   //队尾指针
    int maxSize;    //最大容量
public:
    //有参构造
    Queue(int size):maxSize(size+1){
        queue = new T[maxSize];
        front = rear = 0;
    }

    //析构函数
    ~Queue(){
        delete [] queue;
    }

    //判空
    bool my_empty(){
        return front == rear;
    }

    //判满
    bool my_full(){
        return (rear + 1) % maxSize == front;
    }

    //入队
    void inqueue(T data){
        if (my_full()){
            cout << "入队失败!" << endl;
            return;
        }
        queue[rear] = data;
        rear = (rear + 1) % maxSize;
        cout << "入队成功!" << endl;
    }

    //出队
    void outqueue(){
        if (my_empty()){
            cout << "出队失败!" << endl;
            return;
        }
        front = (front + 1) % maxSize;
        cout << "出队成功!" << endl;
    }

    //获取第一个元素
    T getFront(){
        if (my_empty()){
            cout << "队空!" << endl;
            return NULL;
        }
        return queue[front];
    }

    //获取最后一个元素
    T getRear(){
        if (my_empty()){
            cout << "队空!" << endl;
            return NULL;
        }
        return queue[rear - 1];
    }

    //元素个数
    int getsize(){
        int size = 0;
        while(queue[front]){
            size++;
            front++;
        }
        front = 0;

        return size;
    }

};

int main()
{
    //创建循环顺序队列
    Queue<int> q(5);

    //入队
    q.inqueue(1);
    q.inqueue(2);
    q.inqueue(3);
    q.inqueue(4);
    q.inqueue(5);

    //判满
    if(q.my_full()){
        cout << "队满!" << endl;
    }else{
        cout << "没有队满!" << endl;
    }

    //出队
    q.outqueue();

    //判空
    if(q.my_empty()){
        cout << "队空!" << endl;
    }else{
        cout << "没有队空!" << endl;
    }

    cout << "第一个元素:" << q.getFront() << endl;
    cout << "最后一个元素:" << q.getRear() << endl;
    cout << "队列元素个数:" << q.getsize() << endl;

    return 0;
}

思维导图:

你可能感兴趣的:(c++,开发语言)