顺序栈

今天,我们一起用C++写一个顺序栈,具体如下:

SeqStack.h具体内容如下:

[cpp]  view plain copy
  1. template<typename Type> class SeqStack{  
  2. public:  
  3.     SeqStack(int sz) :m_ntop(-1), m_nMaxSize(sz){  
  4.         m_pelements = new Type[sz];  
  5.         if (m_pelements == NULL){  
  6.             cout << "Application Error!" << endl;  
  7.             exit(1);  
  8.         }  
  9.     }  
  10.     ~SeqStack(){  
  11.         delete[] m_pelements;  
  12.     }  
  13.   
  14. public:  
  15.   
  16.     void Push(const Type item); //push data  
  17.     Type Pop();                 //pop data  
  18.     Type GetTop() const;        //get data  
  19.     void Print();               //print the stack  
  20.     void MakeEmpty(){           //make the stack empty  
  21.         m_ntop = -1;  
  22.     }  
  23.     bool IsEmpty() const{  
  24.         return m_ntop == -1;  
  25.     }  
  26.     bool IsFull() const{  
  27.         return m_ntop == m_nMaxSize - 1;  
  28.     }  
  29.   
  30.   
  31. private:  
  32.     int m_ntop;  
  33.     Type *m_pelements;  
  34.     int m_nMaxSize;  
  35.   
  36. };  
  37.   
  38. template<typename Type> void SeqStack<Type>::Push(const Type item){  
  39.     if (IsFull()){  
  40.         cout << "The stack is full!" << endl;  
  41.         return;  
  42.     }  
  43.     m_pelements[++m_ntop] = item;  
  44. }  
  45.   
  46. template<typename Type> Type SeqStack<Type>::Pop(){  
  47.     if (IsEmpty()){  
  48.         cout << "There is no element!" << endl;  
  49.         //exit(1);  
  50.     }  
  51.     return m_pelements[m_ntop--];  
  52. }  
  53.   
  54. template<typename Type> Type SeqStack<Type>::GetTop() const{  
  55.     if (IsEmpty()){  
  56.         cout << "There is no element!" << endl;  
  57.         //exit(1);  
  58.     }  
  59.     return m_pelements[m_ntop];  
  60. }  
  61.   
  62. template<typename Type> void SeqStack<Type>::Print(){  
  63.     cout << "bottom";  
  64.     for (int i = 0; i <= m_ntop; i++){  
  65.         cout << "--->" << m_pelements[i];  
  66.     }  
  67.     cout << "--->top" << endl << endl << endl;  
  68. }  
main.cpp具体内容如下:

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include <stdlib.h>  
  3. using namespace std;  
  4.   
  5. #include "SeqStack.h"  
  6.   
  7. int main(){  
  8.     SeqStack<int> stack(10);  
  9.     int init[10] = { 1, 2, 6, 9, 0, 3, 8, 7, 5, 4 };  
  10.     for (int i = 0; i < 10; i++)  
  11.     {  
  12.         stack.Push(init[i]);  
  13.     }  
  14.     stack.Print();  
  15.   
  16.     stack.Push(88);  
  17.   
  18.     cout << stack.Pop() << endl;  
  19.     stack.Print();  
  20.   
  21.     stack.MakeEmpty();  
  22.     stack.Print();  
  23.   
  24.     stack.Pop();  
  25.   
  26.     system("pause");  
  27.     return 0;  
  28. }  
运行效果如图1所示:

图1 运行效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值