#include<iostream>
using namespace std
;
class stack
{
int size
;
int *top
;
int *base
;
public:
stack(int n
)
{
top
=base
=new int[n
];
size
=n
;
}
void push(int x
)
{
if(top
-base
<size
)
*top
++=x
;
}
int pop()
{
if(top
>base
)
return *--top
;
else
return 0;
}
int operator!()
{
return (top
==base
);
}
operator int()
{
return size
;
}
int *getbase()
{
return base
;
}
int count(int *base
)
{
static int c
=0;
if(base
<top
)
{
if(*base
++==0)
c
++;
count(base
);
}
return c
;
}
};
int main()
{
stack
p(10);
p
.push(0);
p
.push(1);
p
.push(2);
p
.push(4);
cout
<<"堆栈的长度为:"<<endl
;
int length
=p
;cout
<<length
<<endl
;
if(!(!p
))
{
cout
<<"堆栈中0的个数为:"<<endl
;
int n
=p
.count(p
.getbase());cout
<<n
<<endl
;
}
cout
<<"栈顶元素为:"<<endl
;
int x
=p
.pop();cout
<<x
<<endl
;
}
转载请注明原文地址: https://mac.8miu.com/read-501576.html