该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
/*mystack.h*/
#ifndef MYSTACK_H
#define MYSTACK_H
#define StackMax 10000
template
struct mystack
{
public:
StackType item[StackMax];//Storage container
int now;//Amount
mystack(){now=0;}//Constructor(to initialize 'now')
typedef StackType iterator;//Types of loop variable
void clear()//This function will clear the stack(initialization).
{
while(!empty())
del();
}
void del()//This function will remove top element(Non-return).
{
item[--now]=(StackType)0;
}
void push(StackType num)//This function will insert element.
{
item[now++]=num;
}
StackType pop()//This function will remove top element.
{
StackType res=item[--now];
item[now]=(StackType)0;
return res;
}
StackType top()//This function will return the value.
{
return item[now-1];
}
void swap(mystack&x)//This function will swap contents.
{
int i=now,maxnum=(now>x.now?now:x.now);
now=x.now;
x.now=i;
StackType t;
for(int i=0;i
{
t=item[i];
item[i]=(StackType)x.item[i];
x.item[i]=t;
}
}
bool empty()//This function will test whether container is empty.If it's not empty,it will return 1(true).
{
return now?0:1;
}
int size()//This function will return the amount of the value what is in the stack.
{
return now;
}
bool operator==(const mystack& x)
{
if(now!=x.now)return false;
for(int i=0;i
{
if(item[i]!=(StackType)x.item[i])
return false;
}
return true;
}
bool operator!=(const mystack&x)
{return (!(*this == x)); }
bool operator
{return (now
bool operator>(const mystack&x)
{return (now>x.now); }
bool operator<=(const mystack&x)
{return (now<=x.now); }
bool operator>=(const mystack&x)
{return (now>=x.now); }
mystack&operator=(const mystack& x)
{
now=x.now;
for(int i=0;i
item[i]=(StackType)x.item[i];
return *this;
}
mystack operator+(const mystack& x)//+ will connect two mystack's variables
{
mystack _X;
for(int i=0;i
_X.item[i]=item[i];
for(int j=now;j
_X.item[j]=(StackType)x.item[j-now];
_X.now=now+x.now;
return _X;
}
mystack&operator+=(const mystack& x)//"a+=b" is the same as "a=a+b".
{
*this=*this+x;
return *this;
}
mystack operator+(const StackType& x)//+ is the same as push but it won't change its value.
{
mystack _X;
for(int i=0;i
_X.item[i]=item[i];
_X.now=now;
_X.push(x);
return _X;
}
mystack&operator+=(const StackType& x)//+= is the same as push but it has returned value.
{
push(x);
return *this;
}
};
#endif