数据结构=多种结构比较(1)——栈


//栈 面向对象 +++++++++顺序存储结构  数组  浪费资源
#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
using namespace std;
typedef int  ElemType;
const int MAXSIZE = 100;
class  SqStack
{
public:
	 SqStack();
	~ SqStack();
	void SetEmpty();
	void push( ElemType e);
	ElemType pop();
	void PrintOut();

private:
	ElemType elem[MAXSIZE];
	int top;
};

 SqStack:: SqStack()
{
	top = 0;
}

 SqStack::~ SqStack()
{
}

 void SqStack::SetEmpty() {
	 top = 0;
 }

 void SqStack::push( ElemType e) {
	 if (top == MAXSIZE - 1) {
		 cout << "栈满溢出"<< endl;
	 }
	 else
	 {
		 top++;
		 elem[top] = e;
	 }
 }

 ElemType SqStack::pop() {
	 ElemType x;
	 if (top == 0) {
		 cout << "栈为空!!" << endl;
		 x = 0;
	 }
	 else
	 {
		 x = elem[top];
		 top--;
	 }
	 return x;
 }

 void SqStack::PrintOut() {
	 cout << " PrintOut Data:" << endl;
	 if (top == 0) {
		 cout << " 已是空栈!"<
      
      
       
       = 1; k--) {
			 cout << setw(6) << elem[k] << endl;
		 }
	 }
 }
 int main() {
	 ElemType e, x;
	 int a;
	 SqStack s;
	 cout << "栈的顺序存储结构:" << endl;
	 cout <<"输入1进行入栈,输入2进行出栈,输入-999结束" << endl;
	 cin >> a;
	 while (a!=-999)
	 {
		 if (a == 1) {
			 cout << "输入压栈数据:" << endl;
			 cin >> e;
			 s.push(e);
			 s.PrintOut();
			 cout << "输入指令:" << endl;
			 cin >> a;
		 }
		 if (a == 2) {
			 x = s.pop();
			 cout << "出栈数据为:" <
       
       
         << endl; s.PrintOut(); cout << "输入指令:" << endl; cin >> a; } else { cout << "错误指令!" << endl; cout << "输入指令:" << endl; cin >> a; } } _getch(); return 0; } 
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值