栈的实现

//顺序栈的实现
/*#include<iostream>
#define STACKlength 100
#define ADD 10
#define ERROR 0
#define TRUE 1
using namespace std;

class SX_Stack
{
private:
 int *base;
 int top;
 int stacklength;
public:
 SX_Stack(int n=STACKlength)
 {
  base=new int [n];
  //if(base==NULL)return ERROR;因为用了构造函数,所以没有返回值
  top=-1;
  stacklength=n;
  //return TRUE;
 }
 void DestorySX_Stack()  //销毁栈
 {
  if(stacklength) delete [] base;
 }
 void ClearSX_Stack()   //将栈清空
 {
  top=-1;
 }
 int SX_StackEmpty()    //检测栈是否为空
 {
  if(top==-1){cout<<"The stack is null!"<<endl;return TRUE;}
  else
   return ERROR;
 }
 int GetTop(int e)  //返回栈顶元素
 {
  e=base[0];
  return TRUE;
 }
 int Push(int e)  //进栈
 {
  if(top==stacklength-1)
  {
   int *temp,i;
   temp=new int[stacklength+ADD];
   if(temp==NULL)return ERROR;
   for(i=0;i<stacklength;i++)
    temp[i]=base[i];
   delete [] base;
   base=temp;
   stacklength=stacklength+ADD;
  }
  top++;
  base[top]=e;
  return TRUE;
 }
 int Out(int e)  //出栈
 {
  if(top==-1)return ERROR;
  e=base[top];
  top--;
  return e;
 }
 int Length()
 {
  return (top+1);
 }
 void output()
 {
  if(top==-1){cout<<"The SX_Stack is null!"<<endl;return;}
  cout<<"The length of the SX_Stack is:"<<Length()<<endl;
  cout<<"The content of the SX_Stack is:";
  for(;top>=0;top--)
   cout<<base[top]<<" ";
  cout<<endl;
 }
};
int main()
{
 SX_Stack a;
 while(!a.SX_StackEmpty())
 {
  a.ClearSX_Stack();
 }
 for(int i=0;i<5;i++)
  a.Push(i);
 a.output();
 return 0;
}*/
//顺序栈的数制转换
/*#include<iostream>
#define STACKlength 100
#define ADD 10
#define ERROR 0
#define TRUE 1
using namespace std;

class SX_Stack
{
private:
 int *base;
 int top;
 int stacklength;
public:
 SX_Stack(int n=STACKlength)
 {
  base=new int [n];
  //if(base==NULL)return ERROR;因为用了构造函数,所以没有返回值
  top=-1;
  stacklength=n;
  //return TRUE;
 }
 void DestorySX_Stack()  //销毁栈
 {
  if(stacklength) delete [] base;
 }
 void ClearSX_Stack()   //将栈清空
 {
  top=-1;
 }
 int SX_StackEmpty()    //检测栈是否为空
 {
  if(top==-1){cout<<"The stack is null!"<<endl;return TRUE;}
  else
   return ERROR;
 }
 int GetTop(int e)  //返回栈顶元素
 {
  e=base[0];
  return TRUE;
 }
 int Push(int e)  //进栈
 {
  if(top==stacklength-1)
  {
   int *temp,i;
   temp=new int[stacklength+ADD];
   if(temp==NULL)return ERROR;
   for(i=0;i<stacklength;i++)
    temp[i]=base[i];
   delete [] base;
   base=temp;
   stacklength=stacklength+ADD;
  }
  top++;
  base[top]=e;
  return TRUE;
 }
 int Out(int e)  //出栈
 {
  if(top==-1)return ERROR;
  e=base[top];
  top--;
  return e;
 }
 int Length()
 {
  return (top+1);
 }
 void output()
 {
  if(top==-1){cout<<"The SX_Stack is null!"<<endl;return;}
  cout<<"The length of the SX_Stack is:"<<Length()<<endl;
  cout<<"The content of the SX_Stack is:";
  for(;top>=0;top--)
   cout<<base[top];
  cout<<endl;
 }
};
int main()
{
 SX_Stack a;
 int frag=1;
 while(frag)
 {
 while(!a.SX_StackEmpty())
 {
  a.ClearSX_Stack();
 }
 int n,s;
 cout<<"Please input you want change number is:"; //输入你要转换的数
 cin>>n;
 cout<<"Please input you want to change into system is:"; //输入你想要转换的几进制数
 cin>>s;
 while(n)
 {
  a.Push(n%s);
  n=n/s;
 }
 a.output();
 cout<<"Do you want to continues input?yes is 1,no is 0:";  //控制是否还要输入
 int i;
 cin>>i;
 frag=i;
 }
 return 0;
}*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值