棧的實現——c++


棧(stack),是一種線性存儲結構,它有以下幾個特點:
  (01) 棧中數據是按照"后進先出(LIFO, Last In First Out)"方式進出棧的。
  (02) 向棧中添加/刪除數據時,只能從棧頂進行操作。

棧通常包括的三種操作:push、peek、pop
  push -- 向棧中添加元素。
  peek -- 返回棧頂元素。
  pop  -- 返回並刪除棧頂元素的操作

 

C++的STL中本身就包含了stack類,基本上該stack類就能滿足我們的需求,所以很少需要我們自己來實現。本部分介紹2種C++實現。
1. C++實現一:數組實現的棧,能存儲任意類型的數據。
2. C++實現二:C++的 STL 中自帶的"棧"(stack)的示例。

 

1. C++實現一:數組實現的棧,能存儲任意類型的數據

實現代碼:.h

#ifndef ARRAY_STACK_HXX
#define ARRAY_STACK_HXX

#include <iostream>
#include "ArrayStack.h"
using namespace std;

template<class T> class ArrayStack{
    public:
        ArrayStack();
        ~ArrayStack();

        void push(T t);
        T peek();
        T pop();
        int size();
        int isEmpty();
    private:
        T *arr;
        int count;
};

// 創建“棧”,默認大小是12
template<class T>
ArrayStack<T>::ArrayStack() 
{
    arr = new T[12];
    if (!arr) 
    {
        cout<<"arr malloc error!"<<endl;
    }
}

// 銷毀“棧”
template<class T>
ArrayStack<T>::~ArrayStack() 
{
    if (arr) 
    {
        delete[] arr;
        arr = NULL;
    }
}

// 將val添加到棧中
template<class T>
void ArrayStack<T>::push(T t) 
{
    //arr[count++] = val;
    arr[count++] = t;
}

// 返回“棧頂元素值”
template<class T>
T ArrayStack<T>::peek() 
{
    return arr[count-1];
}

// 返回“棧頂元素值”,並刪除“棧頂元素”
template<class T>
T ArrayStack<T>::pop() 
{
    int ret = arr[count-1];
    count--;
    return ret;
}

// 返回“棧”的大小
template<class T>
int ArrayStack<T>::size() 
{
    return count;
}

// 返回“棧”是否為空
template<class T>
int ArrayStack<T>::isEmpty()
{
    return size()==0;
}

#endif
View Code

測試代碼:.cpp

#include <iostream>
#include "ArrayStack.h"
using namespace std;

int main() 
{
    int tmp=0;
    ArrayStack<int> *astack = new ArrayStack<int>();

    cout<<"main"<<endl;

    // 將10, 20, 30 依次推入棧中
    astack->push(10);
    astack->push(20);
    astack->push(30);

    // 將“棧頂元素”賦值給tmp,並刪除“棧頂元素”
    tmp = astack->pop();
    cout<<"tmp="<<tmp<<endl;

    // 只將“棧頂”賦值給tmp,不刪除該元素.
    tmp = astack->peek();

    astack->push(40);

    while (!astack->isEmpty())
    {
        tmp = astack->pop();
        cout<<tmp<<endl;
    }

    return 0;
}
View Code

結果說明:關於"棧的聲明和實現都在頭文件中"的原因,是因為棧的實現利用了C++模板,而"C++編譯器不能支持對模板的分離式編譯"。這在"數據結構和算法01之 線性表"中已經介紹過了。  程序的實現和邏輯都非常簡單。需要說明的是,采用C++模板實現的;但是,默認數組的大小只有12,而且該實現不支持動態擴展。

 

2. C++實現二:C++的 STL 中自帶的"棧"(stack)的示例

實現代碼:

#include <iostream>
#include <stack>
using namespace std;

/**
 * C++ 語言: STL 自帶的“棧”(stack)的示例。
 *
 * @author skywang
 * @date 2013/11/07
 */
int main ()
{
    int tmp=0;
    stack<int> istack;

    // 將10, 20, 30 依次推入棧中
    istack.push(10);
    istack.push(20);
    istack.push(30);

    // 將“棧頂元素”賦值給tmp,並刪除“棧頂元素”
    istack.pop();

    // 只將“棧頂”賦值給tmp,不刪除該元素.
    tmp = istack.top();

    istack.push(40);

    while (!istack.empty())
    {
        tmp = istack.top();
        istack.pop();
        cout<<tmp<<endl;
    }

    return 0;
}
View Code

 

本文來自http://www.cnblogs.com/skywang12345/p/3562239.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM