C++スタックの使い方とスタックの実現
9233 ワード
#include
#include
using namespace std;
int main()
{
stack <int>stk;
//
for(int i=0;i<50;i++){
stk.push(i);
}
cout<<" :"<while(!stk.empty())
{
cout<cout<<" :"<return 0;
}
#include
#include
using namespace std;
#define MAXSIZE 0xffff
template<class type>
class my_stack
{
int top;
type* my_s;
int maxsize;
public:
my_stack():top(-1),maxsize(MAXSIZE)
{
my_s=new type[maxsize];
if(my_s==NULL)
{
cerr<<" !"<1);
}
}
my_stack(int size):top(-1),maxsize(size)
{
my_s=new type[maxsize];
if(my_s==NULL)
{
cerr<<" !"<1);
}
}
~my_stack()
{
delete[] my_s;
}
//
bool Empty();
//
void Push(type tp);
//
type Top();
//
void Pop();
//
int Size();
};
template<class type>
bool my_stack<type>::Empty()
{
if(top==-1){
return true;
}
else
return false;
}
template<class type>
type my_stack<type>::Top()
{
if(top!=-1)
{
return my_s[top];
}
else
{
cout<<"
";
exit(1);
}
}
template<class type>
void my_stack<type>::Push(type tp)
{
if(top+1else
{
cout<<"
";
exit(1);
}
}
template<class type>
void my_stack<type>::Pop()
{
if(top>=0)
{
top--;
}
else
{
cout<<"
";
exit(1);
}
}
template<class type>
int my_stack<type>::Size()
{
return top+1;
}
#include
#include "my_stack.cpp"
using namespace std;
int main()
{
my_stack<int> stk;
for(int i=0;i<50;i++){
stk.Push(i);
}
cout<<" :"<while(!stk.Empty())
{
cout<cout<<" :"<<sizeof(stk)<return 0;
}
#include
#include
#include "my_stack.cpp"
using namespace std;
int main()
{
my_stack<int> stk;
stack<int> s;
for(int i=0;i<20;i++){
stk.Push(i);
s.push(i);
}
cout<<"mysize()="<"
mysizeof="<<sizeof(stk)<cout<<"size()="<"
sizeof="<<sizeof(s)<return 0;
}
:
mysize()=20
mysizeof=12
size()=20
sizeof=40
また、私が書いたスタッククラスの変数は3つの整数型(1つのtemplate型)しかなく、ちょうど12バイトであることから、c++が提供するスタック内には私が書いたほど簡単ではなく、光変数は40バイトを占めていることがわかります.