作业0903

1.封装栈 

#include <iostream>

using namespace std;

class myStack
{
private:
     int size;  // 大小
     int capacity;
     int *ptr;
     int top;   // 栈顶下标
public:
    // 无参构造函数
    myStack():size(0), top(-1), capacity(10) {ptr = new int[capacity];}
    // 有参构造函数
    myStack(int c):size(0), top(-1), capacity(c) {ptr = new int[capacity];}
    // 析构函数
    ~myStack() {delete []ptr;}
    // 拷贝赋值函数
    myStack& operator=(myStack& other);
    // 判空
    bool empyt();
    // 判满
    bool full();
    // 访问栈顶元素
    int& get_top();
    // 返回容纳的元素数
    int get_size();
    // 向栈顶插入元素
    bool push(int e);
    // 删除栈顶元素
    bool pop();
};

// 拷贝赋值函数
myStack& myStack::operator=(myStack& other)
{
    delete []ptr;
    this->size = other.size;
    this->capacity = other.capacity;
    this->top = other.top;
    this->ptr = new int[capacity];
    memcpy(this->ptr, other.ptr, this->capacity);

    return *this;
}
// 判空
bool myStack::empyt()
{
    return top == -1;
}
// 判满
bool myStack::full()
{
    return top == capacity-1;
}
// 访问栈顶元素
int& myStack::get_top()
{
    return ptr[top];
}
// 返回容纳的元素数
int myStack::get_size()
{
    return size;
}
// 向栈顶插入元素
bool myStack::push(int e)
{
    if(full())
    {
        return false;
    }

    top++;
    ptr[top] = e;
    size++;

    return true;
}
// 删除栈顶元素
bool myStack::pop()
{
    if(empyt())
    {
        return false;
    }

    top--;
    size--;

    return true;
}

int main()
{
    myStack S1(5);
    for(int i=0; i<10; i++)
    {
        S1.push(i+1);
        cout << "size = " << S1.get_size() << endl;
        if(!S1.empyt())
        {
            cout << "S1[top] = " << S1.get_top() << endl;
        }
    }
    cout << "--------------------------" << endl;

    myStack S2 = S1;
    cout << "S2[top] = " << S2.get_top() << endl;
    for(int i=0; i<10; i++)
    {
        S2.pop();
        cout << "size = " << S2.get_size() << endl;
        if(!S2.empyt())
        {
            cout << "S2[top] = " << S2.get_top() << endl;
        }
    }
    cout << "--------------------------" << endl;

    return 0;
}

 

2.封装队列

#include <iostream>

using namespace std;

class myQueue
{
private:
    int front;  // 队首
    int back;   // 队尾
    int size;   // 大小
    int capacity;// 容量
    int *ptr;
public:
    // 无参构造
    myQueue():front(0), back(0), size(0), capacity(10) {ptr = new int[capacity];}
    // 有参构造
    myQueue(int c):front(0), back(0), size(0), capacity(c) {ptr = new int[capacity];}
    // 析构函数
    ~myQueue() {delete []ptr;}
    // 拷贝赋值函数
    myQueue& operator=(myQueue& other);
    // 判空
    bool empty();
    // 判满
    bool full();
    // 访问队首元素
    int& get_front();
    // 访问队尾元素
    int& get_back();
    // 返回容纳的元素数
    int get_size();
    // 向队尾插入元素
    bool push(int e);
    // 删除队首元素
    bool pop();
};

// 拷贝赋值函数
myQueue& myQueue::operator=(myQueue& other)
{
    delete []ptr;
    this->size = other.size;
    this->front = other.front;
    this->back = other.back;
    this->capacity = other.capacity;
    this->ptr = new int[this->capacity];
    memcpy(this->ptr, other.ptr, this->capacity);

    return *this;
}

// 判空
bool myQueue::empty()
{
    return front == back;
}

// 判满
bool myQueue::full()
{
    return (back+1)%capacity == front;
}

// 访问队首元素
int& myQueue::get_front()
{
    return ptr[front];
}

// 访问队尾元素
int& myQueue::get_back()
{
    return ptr[back-1];
}

// 返回容纳的元素数
int myQueue::get_size()
{
    return size;
}

// 向队尾插入元素
bool myQueue::push(int e)
{
    if(full())
    {
        return false;
    }

    ptr[back] = e;
    back = (back+1)%capacity;
    size++;

    return true;
}

// 删除队首元素
bool myQueue::pop()
{
    if(empty())
    {
        return false;
    }

    front = (front+1)%capacity;
    size--;

    return true;
}

int main()
{
    myQueue q1(5);
    cout << "size = " << q1.get_size() << endl;
    cout << "--------------------------" << endl;

    q1.push(666);
    cout << "q1[front] = " << q1.get_front() << endl;
    cout << "q1[back] = " << q1.get_back() << endl;
    cout << "size = " << q1.get_size() << endl;
    cout << "--------------------------" << endl;

    q1.push(777);
    q1.push(888);
    q1.push(999);
    q1.push(111);   // 入队失败
    cout << "q1[front] = " << q1.get_front() << endl;
    cout << "q1[back] = " << q1.get_back() << endl;
    cout << "size = " << q1.get_size() << endl;
    cout << "--------------------------" << endl;

    q1.pop();
    cout << "q1[front] = " << q1.get_front() << endl;
    cout << "q1[back] = " << q1.get_back() << endl;
    cout << "size = " << q1.get_size() << endl;
    cout << "--------------------------" << endl;

    myQueue q2 = q1;
    cout << "q2[front] = " << q2.get_front() << endl;
    cout << "q2[back] = " << q2.get_back() << endl;
    cout << "size = " << q2.get_size() << endl;
    cout << "--------------------------" << endl;

    return 0;
}

思维导图:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值