C++综合系列之模拟栈模版(数组版及单链表版)

一:栈模版(数组版):

stack.h

#ifndef MYSTACK_ARRAY_H
#define MYSTACK_ARRAY_H

#include <iostream>
using namespace std;

template<class T>
class MyStack
{
public:
    MyStack(int capacity) :
        m_capacity(capacity),
        m_top(0)
    {
        m_element = new T[capacity];
    }
    ~MyStack()
    {
        delete []m_element;
    }

    void push(T value); //入栈
    T pop();          //出栈
    T top();          //获取栈顶元素

    int capacity();   //获取容量
    int counts();     //获取栈内元素个数
    bool isEmpty();   //判断栈是否为空
    void print();     //打印栈

private:
    T  *m_element;
    int m_top;
    int m_capacity;
};

template<class T>
void MyStack<T>::push(T value)
{
    if(m_top >= m_capacity)
    {
        cout << "push failed, the stack is full." << endl;
    }
    else
    {
        m_element[m_top++] = value; //如何实现new空间的动态增长?
    }
}

template<class T>
T MyStack<T>::pop()
{
    if(0 == m_top)
    {
        cout << "pop failed, the stack is empty." << endl;

        T value;
        return value;
    }

    return m_element[--m_top];
}

template<class T>
T MyStack<T>::top()
{
    if(0 == m_top)
    {
        cout << "top failed, the stack is empty." << endl;

        T value;
        return value;
    }

    return m_element[m_top-1];
}

template<class T>
int MyStack<T>::capacity()
{
    return m_capacity;
}

template<class T>
int MyStack<T>::counts()
{
    return m_top;
}

template<class T>
bool MyStack<T>::isEmpty()
{
    return m_top;
}

template<class T>
void MyStack<T>::print()
{
    if(0 == m_top)
    {
        cout << "print failed, the stack is empty." << endl;
    }
    else
    {
        for(int i = m_top-1; i >= 0; i--)
        {
            cout << m_element[i] << endl;
        }
    }
}

#endif // MYSTACK_ARRAY_H

main.cpp

#include "mystack_array.h"

int main()
{
    MyStack<char> myStack(2);
    myStack.push('a');
    myStack.push('b');
    myStack.push('c');
    myStack.print();

    myStack.pop();
    myStack.pop();

    myStack.push('e');
    myStack.push('f');
    myStack.print();

    cout << myStack.top() << endl;
    cout << myStack.counts() << endl;
    cout << myStack.isEmpty() << endl;

    return 0;
}

二:栈模版(单链表版):

这里写图片描述

stack.h

#ifndef MYSTACK_LIST_H
#define MYSTACK_LIST_H

#include <iostream>
using namespace std;

template<class T>
struct Node
{
    Node() :
        m_pNext(nullptr)
    {

    }

    Node(T value) :
        m_value(value), m_pNext(nullptr)
    {

    }

    T m_value;
    Node<T> *m_pNext;
};

template<class T>
class MyStack
{
public:
    MyStack()
    {
        m_capacity = 0;
        m_pHead = new Node<T>;
    }
    void push(T value); //入栈
    T pop();          //出栈
    T top();          //获取栈顶元素

    int counts();     //获取栈内元素个数
    bool isEmpty();   //判断栈是否为空
    void print();     //打印栈

private:
    int m_capacity;  //容量
    Node<T> *m_pHead;//栈头
};

template<class T>
void MyStack<T>::push(T value)
{
    Node<T> *pnode = new Node<T>(value);
    pnode->m_pNext = m_pHead->m_pNext;//先将头节点所指向的下一个节点(也就是最后的空节点)赋值给新建节点的下一个节点,使得新建节点的next指针指向最后节点
    m_pHead->m_pNext = pnode;//然后将新建节点赋值给头节点的next指针,使得头结点的next指针指向新建节点
    m_capacity++;
}

template<class T>
T MyStack<T>::pop()
{
    if( nullptr == m_pHead->m_pNext )
    {
        cout << "pop failed, the stack is empty." << endl;

        T value;
        return value;
    }
    else
    {
        Node<T> *temp = m_pHead->m_pNext;//先获取头结点的下一个节点(即所需要出栈的节点)
        m_pHead->m_pNext = m_pHead->m_pNext->m_pNext;//然后将出栈的节点的下一节点赋值给头节点的next指针,即头指针指向出栈节点的下一节点,这样出栈节点就从链表中剔除了

        T value = temp->m_value;
        delete temp;
        temp = NULL;

        m_capacity--;

        return value;
    }

}

template<class T>
T MyStack<T>::top()
{
    if( nullptr == m_pHead->m_pNext )
    {
        cout << "top failed, the stack is empty." << endl;

        T value;
        return value;
    }
    else
    {
        return m_pHead->m_pNext->m_value;
    }
}


template<class T>
int MyStack<T>::counts()
{
    return m_capacity;
}

template<class T>
bool MyStack<T>::isEmpty()
{
    return m_capacity;
}

template<class T>
void MyStack<T>::print()
{
    if( nullptr == m_pHead->m_pNext )
    {
        cout << "print failed, the stack is empty." << endl;

        return;
    }
    else
    {
        Node<T> *temp = m_pHead;
        while(temp->m_pNext != nullptr)
        {
            temp = temp->m_pNext;
            cout << temp->m_value << endl;
        }
    }
}

#endif // MYSTACK_LIST_H

main.cpp

#include "mystack_list.h"

int main()
{
    MyStack<char> myStack;
    myStack.push('a');
    myStack.push('b');
    myStack.push('c');
    myStack.print();

    myStack.pop();
    myStack.pop();

    myStack.push('e');
    myStack.push('f');
    myStack.print();

    cout << myStack.top() << endl;
    cout << myStack.counts() << endl;
    cout << myStack.isEmpty() << endl;

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值