码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构与算法-栈

时间:2019-09-27 22:49:21      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:扩容   arrays   bool   int   array   原理   线性   temp   while   

1.栈的原理

    后进先出(LIFO-last in first out):最后插入的元素最先出来,是一种“操作受限”的线性表,只允许在端插入和删除数据

技术图片

2.栈的实现

    顺序栈:用数组实现,顺序栈需要动态扩容,在初始化时需要给定一个固定大小的数组,当栈大于固定大小时需要扩充数组的大小。

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

public:
    bool empty() const;
    void push(T const&);
    void pop();
    T top() const;

protected:
    T *m_pArrayData;
    int m_nSize;
    int m_nMaxSize;
};

template<class T>
MyArrayStack<T>::MyArrayStack(){
    m_nSize = 0;
    m_nMaxSize = 2;
    m_pArrayData = new T[m_nMaxSize];
}

template<class T>
MyArrayStack<T>::~MyArrayStack(){
    delete []m_pArrayData;
    m_pArrayData = NULL;
}

template<class T>
bool MyArrayStack<T>::empty() const {
    if(m_nSize == 0) {
        return true;
    }
    return false;
}

template<class T>
void MyArrayStack<T>::pop() {
    if(m_nSize <= 0) return ;
    if(m_nSize < m_nMaxSize/2) {
        m_nMaxSize /= 2;
        T *pTmp = new T[m_nMaxSize];
        memcpy(pTmp, m_pArrayData, sizeof(T)*m_nSize);
        delete []m_pArrayData;
        m_pArrayData = NULL;
        m_pArrayData = pTmp;
    }
    if(m_nSize != 0) {
        m_nSize--;
    }
}

template<class T>
void MyArrayStack<T>::push(T const& param) {
    if(m_nSize >= m_nMaxSize) {
        m_nMaxSize *= 2;
        T *pTmp = new T[m_nMaxSize];
        memcpy(pTmp, m_pArrayData, sizeof(T)*m_nSize);
        delete []m_pArrayData;
        m_pArrayData = NULL;
        m_pArrayData = pTmp;
        
    } 
    m_pArrayData[m_nSize++] = param;
}

template<class T>
T MyArrayStack<T>::top() const {
    return m_pArrayData[m_nSize-1];
}

链式栈:用链表实现

template<class T>
struct Node {
    T nNum;
    Node* pNext;
    Node() {
        pNext = NULL;
    }
};

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

public:
    bool empty() const;
    void push(T const&);
    void pop();
    T top() const;
    
protected:
    Node<T>* pHead;
};

template<class T>
MyListStack<T>::MyListStack() {
    pHead = NULL;
}

template<class T>
MyListStack<T>::~MyListStack(){
    while(pHead != NULL) {
        Node<T> *pTmp = pHead->pNext;
        delete pHead;
        pHead = pTmp;
    }
}

template<class T>
bool MyListStack<T>::empty() const {
    if(pHead == NULL) {
        return true;
    }
    return false;
}

template<class T>
void MyListStack<T>::pop() {
    if(pHead != NULL) {
        Node<T> *pTmp = pHead->pNext;
        delete pHead;
        pHead = pTmp;
    }
}

template<class T>
void MyListStack<T>::push(T const& param) {
    if(pHead == NULL) {
        pHead = new Node<T>;
        pHead->nNum = param;
        pHead->pNext = NULL;
    }
    else {
        Node<T> *pTmp = new Node<T>;
        pTmp->nNum = param;
        pTmp->pNext = pHead;
        pHead = pTmp;
    }
}

template<class T>
T MyListStack<T>::top() const {
    return pHead->nNum;
}

技术图片

可关注公众号了解更多的面试技巧

数据结构与算法-栈

标签:扩容   arrays   bool   int   array   原理   线性   temp   while   

原文地址:https://www.cnblogs.com/yew0/p/11600687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!