9月8日作业

发布于:2023-09-16 ⋅ 阅读:(91) ⋅ 点赞:(0)

思维导图

栈stack.h

#ifndef STACK_H
#define STACK_H
#include <iostream>
#define MAXSIZE 128

using namespace std;
class Stack
{
public:
    //构造函数
    Stack();
    //析构函数
    ~Stack();
    //拷贝构造函数
    Stack(const Stack &other);
    //入栈
    bool push(int value);
    //出栈并返回栈顶元素
    int pop();
    //清空栈
    bool clear();
    //判空
    bool isEmpty();
    //判满
    bool isFull();
    //获取栈顶元素
    int stacktop();
    //求栈的大小
    int stacksize();
    //输出栈元素
    bool show();

private:
    int *stackData;
    int topIndex;
};

#endif // STACK_H

stack.cpp

#include"stack.h"

//构造函数
Stack::Stack()
{
    // 使用动态内存分配来创建堆上的栈
    stackData = new int[MAXSIZE];
    topIndex = -1; // 初始化栈顶索引为-1,表示栈为空
}

//析构函数
Stack::~Stack()
{
    delete [] stackData;
}

//拷贝构造函数
Stack::Stack(const Stack &other)
{
    topIndex = other.topIndex;
    stackData = new int[MAXSIZE];

    for (int i = 0; i <= topIndex; i++) {
        stackData[i] = other.stackData[i];
    }
}

//入栈
bool Stack::push(int value)
{
    if(isFull()){
        cout<<"该栈已满,无法插入"<<endl;
        return false;
    }else {
        topIndex++;
        stackData[topIndex] = value;
        return true;
    }
}

//出栈并返回栈顶元素
int Stack::pop()
{
    if(isEmpty()){
        cout<<"该栈为空"<<endl;
        return -1;
    }else {
        int topvalue = stackData[topIndex];
        topIndex--;
        return topvalue;
    }
}

//清空栈
bool Stack::clear()
{
    if(isEmpty()){
        cout<<"该栈为空"<<endl;
        return false;
    }else {
        topIndex = -1;
        return true;
    }
}

//判空
bool Stack::isEmpty()
{
    return topIndex == -1;
}

//判满
bool Stack::isFull()
{
    return topIndex == MAXSIZE-1;
}

//获取栈顶元素
int Stack::stacktop()
{
    return stackData[topIndex];
}

//求栈的大小
int Stack::stacksize()
{
    return topIndex+1;
}

//输出栈元素
bool Stack::show()
{
    if(isEmpty()){
        cout<<"该栈为空"<<endl;
        return false;
    }else {
        for(int i=0;i<=topIndex; i++){
            cout<<stackData[i]<<" ";
        }
        cout<<endl;
        return true;
    }
}

main.cpp

#include"stack.h"

int main()
{
    Stack s1;
    s1.push(5);
    s1.push(5);
    s1.pop();
    s1.show();
    s1.~Stack();
    return 0;
}

循环队列 loopqueue.h

#ifndef LOOPQUEUE_H
#define LOOPQUEUE_H
#include <iostream>
#define MAXSIZE 16

using namespace std;
class LoopQueue
{
public:
    //构造函数
    LoopQueue();

    //析构函数
    ~LoopQueue();

    //拷贝构造函数
    LoopQueue(const LoopQueue &other);

    //入队列
    bool push(int value);

    //出队列并返回队列顶元素
    int pop();

    //清空队列
    bool clear();

    //判空
    bool isEmpty();

    //判满
    bool isFull();

    //求队列的大小
    int LoopQueuesize();

    //输出队列元素
    bool show();

private:
    int *LoopQueueData;
    int FrontIndex;
    int RearIndex;
};
#endif // LOOPQUEUE_H

loopqueue.cpp

#include"LoopQueue.h"

//构造函数
LoopQueue::LoopQueue()
{
    LoopQueueData = new int[MAXSIZE];
    FrontIndex = 0;
    RearIndex = 0;
}

//析构函数
LoopQueue::~LoopQueue()
{
    delete [] LoopQueueData;
}

//拷贝构造函数
LoopQueue::LoopQueue(const LoopQueue &other)
{
    LoopQueueData = new int[MAXSIZE];
    int i=other.FrontIndex;
    while(1)
    {
        LoopQueueData[i] = other.LoopQueueData[i];
        i++;
        if(MAXSIZE==i)
        {
            i=0;
        }
        if(i==other.RearIndex)
        {
            break;
        }
    }
    FrontIndex = other.FrontIndex;
    RearIndex = other.RearIndex;
}

//入队列
bool LoopQueue::push(int value)
{
    if(isFull()){
        cout<<"该队列已满"<<endl;
        return false;
    }else{
        LoopQueueData[RearIndex] = value;
        RearIndex++;
        return true;
    }
}

//出队列并返回队列顶元素
int LoopQueue::pop()
{
    if(isEmpty()){
        cout<<"该队列为空"<<endl;
        return -1;
    }else {
        int Frontvalue = LoopQueueData[RearIndex];
        FrontIndex++;
        return Frontvalue;
    }
}

//清空队列
bool LoopQueue::clear()
{
    if(isEmpty()){
        cout<<"该队列为空"<<endl;
        return false;
    }else {
        FrontIndex = -1;
        RearIndex = -1;
        return true;
    }
}

//判空
bool LoopQueue::isEmpty()
{
    return FrontIndex==RearIndex;
}

//判满
bool LoopQueue::isFull()
{
    return (RearIndex+1)%MAXSIZE==FrontIndex;
}

//求队列的大小
int LoopQueue::LoopQueuesize()
{
    return (RearIndex-FrontIndex+MAXSIZE)%MAXSIZE;
}

//输出队列元素
bool LoopQueue::show()
{
    if(isEmpty()){
        cout<<"该队列为空"<<endl;
        return false;
    }else {
        int i=FrontIndex;
        cout<<"该队列为>>";
        while(1)
        {
            cout<<LoopQueueData[i]<<" ";
            i++;
            if(MAXSIZE==i)
            {
                i=0;
            }
            if(i==RearIndex)
            {
                break;
            }
        }
        cout<<endl;
        return true;
    }
}

main.cpp

#include"LoopQueue.h"

using namespace std;

int main()
{
    LoopQueue l1;
    /*int value = -1;
    while(1){
        cout<<"请输入要插入的值>>> "<<endl;
        cin>>value;
        if(-1==value){
            break;
        }else{
            l1.push(value);
        }
    }*/
    l1.push(23);
    l1.push(44);
    l1.push(39);
    l1.pop();
    l1.show();
    cout<<"队列大小为>>"<<l1.LoopQueuesize()<<endl;
    return 0;
}

本文含有隐藏内容,请 开通VIP 后查看