2024.9.3 作业

自己实现栈和队列

代码:

/*******************************************/

文件名:sq.h

/*******************************************/

#ifndef SQ_H
#define SQ_H
#include <iostream>
#include<cstring>

using namespace std;
class Mystack
{
private:
    char *data;
    size_t size=0;
    size_t cap=10;
    void resize()
    {
        size_t newcap=2*cap;
        char *newdata=new char[newcap];
        for(size_t i=0;i<size;i++)
        {
            newdata[i]=data[i];
        }
        delete[]data;
        data=newdata;
        cap=newcap;
    }
public:
    Mystack():size(0),cap(10){data=new char[cap];}
    ~Mystack()
    {
        delete[]data;
    }
    Mystack &operator=(const Mystack &other);
    char top();
    bool empty()const;
    size_t getsize()const;
    void push(const char &value);
    void pop();
    void show();
};

class Myqueue
{
private:
    char *data;
    int frontIndex; // 队列头部的索引
    int rearIndex;  // 队列尾部的索引
    int capacity;   // 队列的容量
    int count;      // 队列中当前元素的数量

    void resize() {
        char* newData = new char[capacity * 2];
        int i = 0;
        for (int j = frontIndex; i < count; j = (j + 1) % capacity, ++i) {
            newData[i] = data[j];
        }
        delete[] data;
        data = newData;
        frontIndex = 0;
        rearIndex = count;
        capacity *= 2;
    }
public:
    Myqueue():frontIndex(0),rearIndex(0),capacity(10),count(0){data = new char[capacity];}
    ~Myqueue()
    {
        delete[] data;
    }
    Myqueue &operator=(const Myqueue &other);
    char &front();
    char &back();
    bool empty()const;
    size_t size()const;
    void push(const char &value);
    void pop();
    void show();
};

#endif // SQ_H

/*******************************************/

文件名:sq.cpp

/*******************************************/

#include"sq.h"
Mystack &Mystack::operator=(const Mystack &other)
{
    delete[] data;
    size = other.size;
    cap = other.cap;
    data = new char[cap];
    for (size_t i = 0; i < size; ++i) {
        data[i] = other.data[i];
    }
    return *this;
}
char Mystack::top()
{
    return data[size-1];
}
bool Mystack::empty()const
{
    return size==0;
}
size_t Mystack::getsize()const
{
    return cap;
}
void Mystack::push(const char &value)
{
    if (size == cap){
        resize();
    }
    data[size++] = value;
}
void Mystack::pop()
{
    data[size--]=0;
}
void Mystack::show()
{
    for(size_t i=0;i<size;i++)
    {
        cout<<data[i];
    }
    cout<<endl;
}

Myqueue &Myqueue::operator=(const Myqueue &other)
{
    delete[] data;
    count = other.count;
    capacity = other.capacity;
    data = new char[capacity];
    for (int i = 0; i < count; ++i) {
        data[i] = other.data[i];
    }
    return *this;
}
char &Myqueue::front()
{
    return data[frontIndex];
}
char &Myqueue::back()
{
    return data[(rearIndex - 1 + capacity) % capacity];
}
bool Myqueue::empty()const
{
    return count == 0;
}
size_t Myqueue::size()const
{
    return count;
}
void Myqueue::push(const char &value)
{
    if (count == capacity) {
        resize();
    }
    data[rearIndex] = value;
    rearIndex = (rearIndex + 1) % capacity;
    ++count;
}
void Myqueue::pop()
{
    frontIndex = (frontIndex + 1) % capacity;
    --count;
}
void Myqueue::show()
{
    for(int i=0;i<count;i++)
    {
        cout<<data[i];
    }
    cout<<endl;
}

/*******************************************/

文件名:main.cpp

/*******************************************/

#include"sq.h"

int main()
{
    Mystack s1;
    s1.push('a');
    s1.push('a');
    s1.push('a');
    s1.push('a');
    s1.push('a');
    cout<<s1.top()<<endl;
    cout<<s1.getsize()<<endl;
    s1.show();
    Mystack s2;
    s2=s1;
    s2.show();
    s2.pop();
    s2.show();
    Myqueue q1;
    q1.push('b');
    q1.push('b');
    q1.push('b');
    q1.push('b');
    q1.push('c');
    cout<<q1.front()<<endl;
    cout<<q1.back()<<endl;
    cout<<q1.size()<<endl;
    q1.show();
    Myqueue q2;
    q2=q1;
    q2.show();
    q2.pop();
    q2.show();
    return 0;
}

结果:

思维导图:

  • 19
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值