来源:互联网 发布:关联规则算法有哪些 编辑:程序博客网 时间:2024/04/28 11:35

堆栈(英语:stack),也可直接称栈。在计算机科学中,是一种特殊的串行形式的数据结构,它的特殊之处在于只能允许在链结串行或阵列的一端(称为堆栈顶端指标,英语:top)进行加入资料(英语:push)和输出资料(英语:pop)的运算。另外堆栈也可以用一维阵列或连结串行的形式来完成。堆栈的另外一个相对的操作方式称为伫列。

由于堆栈数据结构只允许在一端进行操作,因而按照后进先出(LIFO, Last In First Out)的原理运作。

堆栈数据结构使用两种基本操作:推入(push)和弹出(pop):

  • 推入:将数据放入堆栈的顶端(阵列形式或串行形式),堆栈顶端top指标加一。
  • 弹出:将顶端数据资料输出(回传),堆栈顶端资料减一。

一个栈的简单实现如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #ifndef STACK_H  
  2. #define STACK_H  
  3. #include <iostream>  
  4. using std::ostream;  
  5.   
  6. const int defaultSize = 5;  
  7. const int overSize = 2;  
  8. template <typename T>  
  9. class Stack  
  10. {  
  11.   
  12. public:  
  13.     Stack(int size = defaultSize) : maxSize(size),top(-1)  
  14.     {  
  15.         elements = new T[size];//创建数组  
  16.     }  
  17.   
  18.     ~Stack()  
  19.     {  
  20.         delete []elements;  
  21.     }  
  22.   
  23.     bool isFull() const  
  24.     {  
  25.         return top == (maxSize - 1);  
  26.     }  
  27.   
  28.     bool isEmpty() const  
  29.     {  
  30.         return top == -1;  
  31.     }  
  32.   
  33.     void makeEmpty()  
  34.     {  
  35.         top = -1;  
  36.     }  
  37.   
  38.     int getSize() const  
  39.     {  
  40.         return top+1;  
  41.     }  
  42.   
  43.     void push(T &value)  
  44.     {  
  45.         if(!isFull())  
  46.         {  
  47.             elements[++top] = value;  
  48.         }  
  49.         else//栈满  
  50.         {  
  51.             resize();  
  52.             elements[++top] = value;  
  53.         }  
  54.     }  
  55.   
  56.     bool pop(T &t)  
  57.     {  
  58.         if(!isEmpty())  
  59.         {  
  60.             t = elements[top--];  
  61.             return true;  
  62.         }  
  63.         else//栈空  
  64.         {  
  65.             return false;  
  66.         }  
  67.     }  
  68.   
  69.     bool getTop(T &t) const  
  70.     {  
  71.         if(!isEmpty())  
  72.         {  
  73.             t = elements[top];  
  74.             return true;  
  75.         }  
  76.         else//栈空  
  77.         {  
  78.             return false;  
  79.         }  
  80.     }  
  81.   
  82.     friend ostream & operator<< (ostream &out,const Stack<T> &s)  
  83.     {  
  84.         out << "the stack top is " << s.top << endl;  
  85.   
  86.         for(int i = 0; i <= s.top; i++)  
  87.         {  
  88.             cout <<" "<<s.elements[i] ;  
  89.         }  
  90.         out << endl;  
  91.   
  92.         return out;  
  93.     }  
  94.   
  95. private:  
  96.     T *elements;  
  97.     int top;  
  98.     int maxSize;  
  99.   
  100.     void resize()//每次栈满后需对栈进行扩充   
  101.     {  
  102.         T *newElements = new T[maxSize+overSize];  
  103.         for(int i = 0; i < maxSize;i++)  
  104.         {  
  105.             newElements[i] = elements[i];//拷贝原来的数据  
  106.         }  
  107.   
  108.         maxSize += overSize;  
  109.         delete []elements;  
  110.         elements = newElements;  
  111.           
  112.     }  
  113. };  
  114.   
  115.   
  116.   
  117.   
  118. #endif  

测试代码如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stack.h"  
  2. #include <fstream>  
  3. using std::cin;    
  4. using std::cout;    
  5. using std::endl;    
  6.   
  7. using std::ifstream;  
  8.   
  9. int main()  
  10. {  
  11.     Stack<int> sta;  
  12.     ifstream fin("data.txt");  
  13.       
  14.     int data;  
  15.     while (!fin.eof()){  
  16.         fin >> data;  
  17.         sta.push(data);       
  18.     }  
  19.     cout << "The initial Stack in the file is:\n" << sta;  
  20.     cout << "The current size of the Stack is: " << sta.getSize() << endl;  
  21.     sta.getTop(data);  
  22.     cout << "The current Top element of the Stack is : " << data << endl;  
  23.     sta.pop(data);  
  24.     cout << "\nDo a Pop operation, then the stack is:\n" << sta << endl;  
  25.     cout << "The data popped is: " << data << endl;  
  26.     sta.getTop(data);  
  27.     cout << "The current Top element of the Stack is : " << data << endl;  
  28.   
  29.     cout << "\nTest the state of the stack:\n";  
  30.     if (sta.isEmpty())  cout << "The stack is empty now!\n";  
  31.     else if (sta.isFull())  cout << "The stack is full now!\n";  
  32.     else    cout << "The stack is not empty and not full now!\n";  
  33.     cout << "Now make the stack empty, then the state of the stack is:\n";  
  34.     sta.makeEmpty();  
  35.     if (sta.isEmpty())  cout << "The stack is empty now!\n";  
  36.     else if (sta.isFull())  cout << "The stack is full now!\n";  
  37.     else    cout << "The stack is not empty and not full now!\n";  
  38.   
  39.     system("pause");  
  40.     return 0;  
  41. }  

0 0
原创粉丝点击