C++DAY4

封装顺序栈类

C++DAY4_第1张图片


#ifndef TEST10_H
#define TEST10_H

#include 

using namespace std;

class Stack
{
private:
    int *ptr;
    int top;
    int maxsize;
public:
    //无参构造
    Stack():ptr(new int[10]),top(-1),maxsize(10) {cout << "无参构造" << endl;}
    //析构函数
    ~Stack();
    //判空函数
    bool stack_empty();
    //判满函数
    bool stack_full();
    //入栈函数
    int push(int data);
    //出栈函数
    int pop();
    //遍历栈函数
    void output();
    //返回栈顶元素的引用
    int &stack_top();
};
#endif // TEST10_H

************************************************************


#include "test10.h"

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

bool Stack::stack_empty()
{
    return (-1 == top);
}


bool Stack::stack_full()
{
    return (maxsize-1 == top);
}

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

int Stack::pop()
{
    if(stack_empty())
    {
        cout << "栈空" << endl;
        return -1;
    }

    return ptr[top--];
}

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

int &Stack::stack_top()
{
    return ptr[top];
}


******************************************************




#include "test10.h"


int main()
{
    Stack 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;
}

思维导图

C++DAY4_第2张图片

封装循环队列

C++DAY4_第3张图片

 

#ifndef TEST11_H
#define TEST11_H


#include 

using namespace std;

class Queue
{
private:
    int *ptr;
    int front;
    int rear;
    int maxsize;
public:
    //无参构造
    Queue():ptr(new int[10]),front(0),rear(0),maxsize(10) {cout << "无参构造" << endl;}
    //析构函数
    ~Queue();
    //判空函数
    bool queue_empty();
    //判满函数
    bool queue_full();
    //入队函数
    int enqueue(int data);
    //出队函数
    int dequeue();
    //遍历函数
    void output();

};


#endif // TEST11_H


************************************************

#include "test11.h"

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

bool Queue::queue_empty()
{
    return (front == rear);
}


bool Queue::queue_full()
{
    return (front == (rear+1)%maxsize);
}

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

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

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


***************************************************
#include "test11.h"


int main()
{
    Queue 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();


    return 0;
}


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