stack
#include <iostream>
using namespace std;
#define MAX 20
template<typename T>
class Mystack
{
private:
int top;
//T *data;
T data[20];
public:
// Mystack(int size=MAX){
// top=-1;
// data=new T[size];
// }
Mystack(){
top=-1;
}
void push(T a)
{
this->top++;
this->data[top]=a;
}
void show()
{
for(int i=0;i<=top;i++)
{
cout<<data[i]<<" ";
}cout<<endl;
}
void pop()
{
this->top--;
}
bool empty()
{
return top==-1?true:false;
}
bool full()
{
return top==(MAX-1)?true:false;
}
};
int main()
{
cout << "Hello World!" << endl;
Mystack<int> s1;
for(int i=0;i<10;i++)
{
s1.push(i);
}
s1.show();
s1.pop();
s1.show();
cout<<"empty? "<<boolalpha<<s1.empty()<<endl;
cout<<"full? "<<boolalpha<<s1.empty()<<endl;
//cout<<s1.data[2]<<endl;
return 0;
}
queue
#include <iostream>
using namespace std;
#define MAX 20
template<typename T>
class Myqueque
{
private:
T data[MAX];
// int front;
// int tail;
T *front;
T *tail;
public:
Myqueque(){
// front=0;
// tail=0;
front=new T(0);
tail=new T(0);
}
bool empty()
{
return front==tail?true:false;
}
bool full()
{
return (*tail+1)%MAX==*front?true:false;
}
void push(T a)
{
if(this->full())
return;
data[*tail]=a;
*tail=(*tail+1)%MAX;
}
void show()
{
for(int i=*front;i!=*tail;i=(i+1)%MAX)
{
cout<<data[i]<<" ";
}cout<<endl;
}
void pop()
{
*front=(*front+1)%MAX;
}
};
int main()
{
cout << "Hello World!" << endl;
Myqueque<int> q1;
for(int i=0;i<5;i++)
{
q1.push(i);
}
q1.show();
q1.pop();
q1.show();
cout<<"empty? "<<boolalpha<<q1.empty()<<endl;
cout<<"full? "<<boolalpha<<q1.empty()<<endl;
return 0;
}