数据结构——栈(C++实现)

来源:互联网 发布:android源码语法 编辑:程序博客网 时间:2024/05/29 15:20

什么是栈?

栈是一种线性数据结构,存贮以及查找数据时只能访问栈的一端。即后进先出(LIFO,last in first out)结构。
栈的主要操作:

  • clear()——清空栈
  • isEmpty()——判断栈是否为空
  • push(el)——将元素el压进栈中
  • pop()——弹出栈顶部的元素
  • topEl()——获取栈顶部的元素,但不删除该元素

最重要的就是push和pop。也是与其他数据结构最不同的地方。

代码示例(C++实现)

代码实现方法有2种,一种是数组实现,一种是链表实现。代码分别如下:
数组实现:

#include "stdafx.h"#include<vector>#include<iostream>using namespace std;
template<class T,int capacity=30>//容量是30class Stack{public:    Stack(){        pool.reserve(capacity);    }    void clear() {        pool.clear();    }    bool isEmpty() const {        return pool.empty();    }    T& topEl() {        return pool.back();    }    T pop() {        T el = pool.back();        pool.pop_back();        return el;    }    void push(const T& el) {        pool.push_back(el);    }private:    vector<T> pool;};
int main(){    Stack<int> st;    st.push(1);    st.push(2);    int e = st.topEl();    cout << e << endl;    st.pop();    st.pop();    cout << st.isEmpty() << endl;    return 0;}

链表实现:
与数组实现类似,不同之处在于容量不受限。

template<class T>class LLStack {public:    LLStack() {     }    void clear() {        lst.clear();    }    bool isEmpty() const {         return lst.empty();      }    T& topEl() {         return lst.back();    }    T pop() {         T el = lst.back();        lst.pop_back();          return el;    }    void push(const T& el) {         lst.push_back(el);    }private:    list<T> lst;};

标准模版库中的栈

使用需引入:

#include<stack> //头文件

默认情况下,deque是底层容器,按底层容器的不同,使用时可以声明为:

//例子: stack<int> stack1;  //默认为双端队列 stack<int,vector<int>> stack2; //向量 stack<int,list<int>> stack3; //链表

代码示例:

#include "stdafx.h"#include<vector>#include<iostream>#include<stack>using namespace std;int main(){    stack<int> m_stack;    m_stack.push(1);    m_stack.push(2);    int t = m_stack.top();    int n = m_stack.size();    cout << t << endl;    cout << n << endl;    return 0;}

STL源码如下:

template<class _Ty,    class _Container = deque<_Ty> >    class stack    {   // LIFO queue implemented with a containerpublic:    typedef stack<_Ty, _Container> _Myt;    typedef _Container container_type;    typedef typename _Container::value_type value_type;    typedef typename _Container::size_type size_type;    typedef typename _Container::reference reference;    typedef typename _Container::const_reference const_reference;    static_assert(is_same<_Ty, value_type>::value, "container adaptors require consistent types");    stack()        : c()        {   // construct with empty container        }    explicit stack(const _Container& _Cont)        : c(_Cont)        {   // construct by copying specified container        }    template<class _Alloc,        class = typename enable_if<uses_allocator<_Container, _Alloc>::value,            void>::type>        explicit stack(const _Alloc& _Al)        : c(_Al)        {   // construct with allocator        }    template<class _Alloc,        class = typename enable_if<uses_allocator<_Container, _Alloc>::value,            void>::type>        stack(const _Myt& _Right, const _Alloc& _Al)        : c(_Right.c, _Al)        {   // construct by copying specified container        }    template<class _Alloc,        class = typename enable_if<uses_allocator<_Container, _Alloc>::value,            void>::type>        stack(const _Container& _Cont, const _Alloc& _Al)        : c(_Cont, _Al)        {   // construct by copying specified container        }    explicit stack(_Container&& _Cont)        : c(_STD move(_Cont))        {   // construct by moving specified container        }    template<class _Alloc,        class = typename enable_if<uses_allocator<_Container, _Alloc>::value,            void>::type>        stack(_Myt&& _Right, const _Alloc& _Al)        : c(_STD move(_Right.c), _Al)        {   // construct by moving specified container        }    template<class _Alloc,        class = typename enable_if<uses_allocator<_Container, _Alloc>::value,            void>::type>        stack(_Container&& _Cont, const _Alloc& _Al)        : c(_STD move(_Cont), _Al)        {   // construct by moving specified container        }    void push(value_type&& _Val)        {   // insert element at beginning        c.push_back(_STD move(_Val));        }    template<class... _Valty>        void emplace(_Valty&&... _Val)        {   // insert element at beginning        c.emplace_back(_STD forward<_Valty>(_Val)...);        }    bool empty() const//是否是空栈        {   // test if stack is empty        return (c.empty());        }    size_type size() const//栈的大小        {   // test length of stack        return (c.size());        }    reference top()//返回栈顶元素值        {   // return last element of mutable stack        return (c.back());        }    const_reference top() const        {   // return last element of nonmutable stack        return (c.back());        }    void push(const value_type& _Val)//入栈        {   // insert element at end        c.push_back(_Val);        }    void pop()//出栈        {   // erase last element        c.pop_back();        }    const _Container& _Get_container() const        {   // get reference to container        return (c);        }    void swap(_Myt& _Right)        _NOEXCEPT_OP(_Is_nothrow_swappable<_Container>::value)        {   // exchange contents with _Right        _Swap_adl(c, _Right.c);        }protected:    _Container c;   // the underlying container    };//底层容器:deque(默认)、vector、list
原创粉丝点击