顺序栈代码如下
#pragma once
template
class SqStack
{
public:
SqStack(int m);
~SqStack();
void Clear();
bool IsEmpty()const;
int Length()const;
T& Top()const;
void Push(const T& e);
void Pop();
private:
T* m_base;
int m_top;
int m_size;
};
template
SqStack
{
m_top=0;
m_base=new T[m];
m_size=m;
}
template
SqStack
{
if(m_base!=NULL)
delete[] m_base;
}
template
void SqStack
{
m_top=0;
}
template
bool SqStack
{
return m_top==0;
}
template
int SqStack
{
return m_top;
}
template
T& SqStack
{
return m_base[m_top-1];
}
template
void SqStack
{
if(m_top>=m_size)
{
T* newbase;
newbase=new T[m_size+10];
for(int j=0;j
delete[] m_base;
m_base=newbase;
m_size+=10;
}
m_base[m_top++]=e;
}
template
void SqStack
{
m_top--;
}
#include
#include"SqStack.h"
using namespace std;
int main()
{
SqStack
if(sq.IsEmpty())
cout<<"Now the stack is empty,its length is "<
cin>>n;
cout<<"input "<
cin>>number;
sq.Push(number);
}
cout<<"Now the length is "<
cout<<"after popping a element,length is "<
cout<<"after clearing the stack,length is "<
return 0;
}