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;
}