模板模拟实现stack和queue

在这里我们接下来实现两个数据结构,一个是stack,一个是queue

stack和queue数据特性?


毫无疑问,我想每一个学过数据结构的人都可以脱口而出,栈的数据特性采用的是先进后出,而队列采用的时候先进先出。
这里写图片描述
这里写图片描述

stack和queue采用什么类型的数据结构?


我们来这样思考,对于栈来说,我们最多需要操作的是栈顶,如果我们采用链式的方式,就需要从头一直遍历到尾部,然后才能再次进行维护。而如果我们采用顺序的结构,直接可以通过索引来进行访问,效率会更高。所以对于栈来说我们采用顺序的存储方式最好的。

然后一个问题是对于队列,我们对队列采用最多的是头删和尾插,这就类似于list操作当中的pushback和popfront,维护起来方便,而对于顺序存储方式,如果我们要头删,我们需要把所以后面的元素都向前移动,这样效率会降低。

了解了我们所需要采取的数据结构类型以后,剩下的就简单了,和以前利用模板实现链表和顺序表类似,进行写代码。

示例代码:


stacak.h

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<cstdlib>
#include<cassert>
using namespace std;
template<typename T>
class Stack
{
public:
    Stack()
        :_pdata(NULL)
        , _sz(0)
        , _capacity(0)
    {

    }
    void Push(const T& d)
    {
        CheckCapacity();
        _pdata[_sz++] = d;
    }
    void Pop()
    {
        assert(_sz > 0);
        _sz--;
    }
    T Top()
    {
        assert(_sz > 0);
        return _pdata[_sz-1];
    }
    size_t Size()
    {
        return _sz;
    }
    bool Empty()
    {
        if (_sz == 0)
            return true;
        else
            return false;
    }
protected:
    void CheckCapacity()
    {
        if (_sz == _capacity)
        {
            size_t NewCapacity = _capacity * 2 + 1;
            T* Temp = new T[NewCapacity];
            //在这里需要注意考虑自定义类型和内置类型的区别,保险期间,我采用下面的方式,另外的解决方法是——类型萃取
            for (size_t i = 0; i < _sz; i++)
            {
                Temp[i] = _pdata[i];
            }
            delete _pdata;
            _pdata = Temp;
            _capacity = NewCapacity;
        }
    }
protected:
    T * _pdata;
    size_t _sz;
    size_t _capacity;
};

queue.h

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<cstdlib>
#include<cassert>

using namespace std;
template <typename T>
struct QueueNode
{
public:
    QueueNode()
        :_next(NULL)
        , data(0)
    {}

    QueueNode *_next;
    T data;
};
template<typename T>
class Queue
{
public:
    Queue()
        :_head(NULL)
        , _tail(NULL)
    {}
    ~Queue()
    {
        QueueNode<T>* cur = _head;
        while (cur != NULL)
        {
            QueueNode<T>* del=cur;
            cur = cur->_next;
            delete del;
            del = NULL;
        }
    }
    bool empty()
    {
        if (_head == NULL)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    size_t size()
    {
        size_t count(0);
        QueueNode<T> *cur = _head;
        while (cur != NULL)
        {
            count++;
            cur = cur->_next;
        }
        return count;
    }
    T& front()
    {
        assert(_head != NULL); 
        return _head->data;
    }
    T& back()
    {   
        assert(_tail != NULL);
        return _tail->data;
    }
    void push(const T& d)
    {
        if (_head == NULL)
        {
            _head = new QueueNode<T>;
            _head->data = d;
            _tail = _head;
        }
        else
        {
            QueueNode<T> *tmp = new QueueNode <T>;
            tmp->data = d;
            tmp->_next = NULL;
            _tail->_next = tmp;
            _tail = _tail->_next;
        }
    }
    void pop()
    {
        assert(_head != NULL);
        QueueNode<T> *del = _head;
        _head = _head->_next;
        delete del;
        del = NULL;

    }
protected:
    QueueNode<T>* _head;
    QueueNode<T>* _tail;
};

记得注意模板的书写

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值