栈和队列

栈的定义–Stack
栈是只允许在末端进行插入和删除的线性表。栈具有后进先出的特性(LIFO,Last In First Out)。

栈的应用
1.算术表达式求值。波兰表达式(后缀表达式)
2.迷宫问题
3.栈模拟压栈,实现递归转非递归。

#pragma once

// 静态栈
//template<class T, size_t N = 100>
//class Stack
//{
//public:
//  void Push(const T& x)
//  {
//      if (_size == N)
//      {
//          throw out_of_range("stack is full");
//      }
//
//      _a[_size++] = x;
//  }
//
//  void Pop()
//  {
//      assert(_size > 0);
//      --_size;
//  }
//
//  T& Top()
//  {
//      return _a[_size-1];
//  }
//protected:
//  T _a[N];
//  size_t _size;
//};

template<class T>
class Stack()
{
public:
    Stack()
        :_a(NULL)
        ,_size(0)
        ,_capacity(0)
    {}

    void Push(const T& x)
    {
        CheckCapacity();

        _a[_size++] = x;
    }

    void Pop()
    {
        assert(_size > 0);
        --_size;
    }

    T& Top()
    {
        assert(_size > 0);
        return _a[_size-1];
    }

    void CheckCapacity()
    {
        if (_size >= _capacity)
        {
            _capacity = _capacity*2+3;
            T* tmp = new T[_capacity];
            if (_a)
            {
                for (size_t i = 0; i < _size; ++i)
                    tmp[i] = _a[i];

                delete[] _a;
                _a = tmp;
            }
        }
    }

protected:
    T* _a;
    size_t _size;
    size_t _capacity;
};

队列的定义
队列只允许在表的队尾进行插入,在表队头进行删除。队列具有先进先出的特性。(FIFO,first In First Out)

优先级队列的定义
每次从队列中取出的都应是最高优先级的数据,这种队列就是优先级队列。 –后续使用Heap实现

队列的应用
1. 生产者消费者模型,如网络数据buffer
2. 任务队列
3. 图的广度优先遍历

#pragma once

template<class T>
struct QueueNode
{
    T _data;
    QueueNode<T>* _next;
};

#include <class T>
class Queue
{
    typedef QueueNode<T> Node;
public:
    Queue()
        :_head(NULL)
        ,_tail(NULL)
    {}

    void Push(const T& x)
    {
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }
        else
        {
            _tail->_next = new Node(x);
            _tail = _tail->_next;
        }
    }

    void Pop()
    {
        if (_head == NULL)
        {
            return;
        }
        else if ( _head == _tail)
        {
            delete _head;
            _head = _tail = NULL;
        }
        else
        {
            Node* next = _head->_next;
            delete _head;
            _head = next;
        }
    }

    bool Empty()
    {
        return _head == NULL;
    }

    size_t Size()
    {
        size_t size = 0;
        Node* cur = _head;
        while (cur)
        {
            ++size;
            cur = cur->_next;
        }

        return size;
    }

protected:
    Node* _head;
    Node* _tail;
};

void TestQueue()
{
    Queue<int> q;
    q.Push(1);
    q.Push(2);
    q.Push(3);
    q.Push(4);

    // O(N^2)
    // N N-1 N-2 ...1
    //while(q.Size() > 0)
    while(!q.Empty())
    {
        //cout<<q.Front()<<" ";
        q.Pop();
    }
    cout<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值