7.18 作业

 1.自己写的stack

mystack.cpp

#include "mystack.h"

MyStack::MyStack():ptr(new int[SIZE]),top(-1){}

MyStack::~MyStack()
{
    delete []ptr;
}

bool MyStack::isempty()
{
    return top==-1;
}

bool MyStack::isfull()
{
    return top==SIZE-1;
}

bool MyStack::push(int t)
{
    if(isfull()) return -1;
    ptr[++top] = t;
    return 0;
}

bool MyStack::pop(int &t)
{
    if(isempty()) return -1;
    t = ptr[top--];
    return 0;
}

void MyStack::show()
{
    if(isempty()) return;
    for(int i = 0;i<=top;++i)
        cout<<ptr[i]<<" ";
    cout<<endl;
}

int &MyStack::gettop()
{
    if(isempty()) return ptr[0];
    return ptr[top];
}

mystack.h

#ifndef MYSTACK_H
#define MYSTACK_H
#include <iostream>
using namespace std;
#define SIZE 10

class MyStack
{
    int *ptr;
    int top;
public:
    MyStack();//构造
    ~MyStack();//析构
    bool isempty();//判空
    bool isfull();//判满
    bool push(int t);//入栈
    bool pop(int &t);//出栈
    void show();//打印
    int &gettop();//头节点的引用
};

#endif // MYSTACK_H

2.自己写的qurue

myqueue.h

#ifndef MYQUEUE_H
#define MYQUEUE_H
#include <iostream>
using namespace std;
#define SIZE 10

class myqueue
{
    int *ptr;
    int front;
    int back;
public:
    myqueue();//构造
    ~myqueue();//析构
    bool isempty();//判空
    bool isfull();//判满
    bool enqueue(int t);//入队
    bool dequeue(int &t);//出队
    int getlen();//长度
};

#endif // MYQUEUE_H
#include "myqueue.h"

myqueue::myqueue():ptr(new int[SIZE]),front(0),back(0){}

myqueue::~myqueue()
{
    delete [] ptr;
}

bool myqueue::isempty()
{
    return front == back;
}

bool myqueue::isfull()
{
    return (front+1)%SIZE == back;
}

bool myqueue::enqueue(int t)
{
    if(isfull()) return -1;
    ptr[back] = t;
    back = (back+1)%SIZE;
    return 0;
}

bool myqueue::dequeue(int &t)
{
    if(isempty()) return -1;
    t = ptr[front];
    front = (front+1)%SIZE;
    return 0;
}

int myqueue::getlen()
{
    return (back - front + SIZE)%SIZE;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值