顺序栈的测试实验《两栈共享一个数组空间》

//SeqStack
const int StackSize=10;
template<class DateType>
 class SeqStack
  {
  private:
	  DateType date[StackSize];
	  int Top1,Top2;
  public:
	  SeqStack();//构造栈;
	  ~SeqStack();//释放栈空间;
	  void push();//入栈
	void  pop();//出栈;
    void Getpop();//获取栈顶元素;
	  int Enpty();//判断是否为空栈;
   };
#include<iostream>
#include"SeqStack.h"
using namespace std;
template<class DateType>//1
 SeqStack<DateType>::SeqStack()
  {
	  Top1=-1;
	  Top2=StackSize;

  }

 template<class DateType>//2
 SeqStack<DateType>::~SeqStack(){}

 template<class DateType>//3
 void SeqStack<DateType>::push()
  {
	  int s=1;
	  int i,x;
	  do{
	     if(Top1==Top2-1)
		  throw "上溢";
		  cout<<"在Top1入栈输入1,Top2入栈输入2"<<endl; 
	     cin>>i;
		 cout<<"输入你要插入的数"<<endl;
		cin>>x;
	  if(i==1)
	  {
        date[++Top1]=x;
	  }

	  else if(i==2)
	  {
		  date[--Top2]=x;

	  }
	  else cout<<"请正确输入数子!!此次操作失败"<<endl;
	  cout<<"是否要继续入栈!!是的话输入1,否则输入其他数字"<<endl;
		  cin>>s;
	  }while(s==1);
  
    }
  template<class DateType>//4
  void SeqStack<DateType>::pop()
   {
	   
	   if( Top1==-1&&Top2==StackSize)
		   throw "空栈";
	     int i,s=1;
	   do{
	   cout<<"从Top1出栈请输入1,Top2出栈请输入2"<<endl;
	   cin>>i;
	   if(i==1)
	   {
		  if(Top1==-1)
		  throw "栈1为空!!";
		  cout<<date[Top1]<<endl;
		  Top1--;

	   }
	   else if(i==2)
	   {
		   if(Top2==StackSize)
			   throw "栈2为空";
		   cout<<date[Top2]<<"  ";
		   Top2++;
       }
	   else cout<<"请正确输入数字!!此次操作失败"<<endl;
       cout<<"是否要继续出栈!!是的话输入1,否则输入其他数字"<<endl;
			cin>>s;
	   }while(s==1);
	   cout<<endl;
   }

   template<class DateType>//5
  int SeqStack<DateType>::Enpty()
   {
     cout<<"1代表栈不为空,0代表为空栈:"<<endl;
	  if( Top1==-1&&Top2==StackSize)
		  return 0;
	  else return 1;
   }
    
   template<class DateType>//6
 void SeqStack<DateType>::Getpop()
  {
	  int i;
	  cout<<"获取栈1中的栈顶元素请输入1,获取栈2栈顶元素请输入2"<<endl;
	  cin>>i;
	  if(i==1)
		  cout<<date[Top1]<<endl;
	  else if(i==2)
		  cout<<date[Top2]<<endl;
	  else cout<<"请正确输入数字!!,此次操作失败"<<endl;

   }
  #include<iostream>
  #include"SqeStack实现细节.cpp"
  using namespace std;
 int main()
  {
      
	  SeqStack<int> seq;
       cout<<"**************************"<<endl;
	   cout<<"使用栈"<<endl;
	   cout<<"**************************"<<endl;
	   cout<<endl;
	   cout<<"................."<<endl;
	   try
	   {seq.push();}
	   catch(char name[])
	   {
		   cout<<name<<endl;
	   }
	   cout<<"................."<<endl;
	   cout<<endl;
	   
	  cout<<"....................."<<endl;
	   int y=seq.Enpty();
	  cout<<"栈是:"<<y<<endl;
	  
	   cout<<"...................."<<endl;
	   cout<<endl;
	   cout<<"...................."<<endl;
	  try
	   {
		   seq.pop();
	   }
	   catch(char name[])
	   {
		   cout<<name<<endl;
	   }
	    cout<<".................."<<endl;
	   seq.Getpop();

	  system("pause");
	  return 0;
  }

    

   

猜你喜欢

转载自blog.csdn.net/huangchongwen/article/details/49227627