栈
#include <iostream>
using namespace std;
class my_stack
{
private:
int *num;
int size;
int len=0;
int top;
public:
my_stack(){};
my_stack(int si):size(si),top(-1){
num=new int[size];
}
~my_stack(){}
my_stack &operator=( int *x){
for(int i=0;i<10;i++)
{
num[++top]=x[i];
len++;
}
return *this;
}
void Top()
{
cout<<"栈顶元素是"<< num[top]<<endl;
}
void Empty(){
if(len==0)
{
cout<<"栈为空"<<endl;
}
else
{
cout<<"栈不为空"<<endl;
}
}
int Size()
{
return len;
}
void Push(int n)
{
num[++top]=n;
len++;
}
void Pop()
{
top--;
len--;
}
void show()
{
for(int i=0;i<len;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
}
};
int main()
{
my_stack s1(50);
int a[10]={1,2,3,4,5,6,7,8,9,10};
s1=a;
s1.show();
cout<<"容纳的元素个数为"<<s1.Size()<<endl;
s1.Empty();
s1.Top();
s1.Push(666);
s1.Pop();
s1.show();
return 0;
}
队列
#include <iostream>
using namespace std;
class my_stack
{
private:
int *num;
int size;
int len=0;
int top;
int ptr;
public:
my_stack(){};
my_stack(int si):size(si),top(-1),ptr(0){
num=new int[size];
}
~my_stack(){}
my_stack &operator=( int *x){
for(int i=0;i<10;i++)
{
num[++top]=x[i];
len++;
}
return *this;
}
void Top()
{
cout<<"链尾元素是"<< num[top]<<endl;
}
void Empty(){
if(len==0)
{
cout<<"链为空"<<endl;
}
else
{
cout<<"链不为空"<<endl;
}
}
int Size()
{
return len;
}
void Push(int n)
{
num[++top]=n;
len++;
}
void Pop()
{
ptr++;
len--;
}
void show()
{
for(int i=ptr;i<len+ptr;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
}
};
int main()
{
my_stack s1(50);
int a[10]={1,2,3,4,5,6,7,8,9,10};
s1=a;
s1.show();
cout<<"容纳的元素个数为"<<s1.Size()<<endl;
s1.Empty();
s1.Top();
s1.Push(666);
s1.show();
s1.Pop();
s1.show();
return 0;
}