栈与队列

目录

  1. 队列&优先队列
  2. <stack>头文件
  3. <queue>头文件
  4. <deque>头文件

1.栈

栈是一种动态集合,且在其上进行delete操作所移除的元素是最近插入的元素。 ——《算法导论》

即所谓“后进先出(Last-in,first-out)”

1.数组实现

PS:以下实现是自己写的如果有纰漏还请赐教

#include <exception>

#define MAXSIZE 1000

template < class T >
class Stack_Realize_By_Array {
private:
    T count;
    T s[MAXSIZE];
public:
    Stack_Realize_By_Array() :count(0) {
        int i;
        for (i = 0; i < MAXSIZE; i++)
        {
            s[i] = T();
        }
    }
    ~Stack_Realize_By_Array() {};
    bool empty() { return (count == 0); }
    bool full() { return (count == MAXSIZE); }

    T top() { return s[count]; }

    bool pop() {
        if (empty())
        {
            throw std::out_of_range("Underflow");
        }
        else
        {
            s[count] = T();
            count--;
        }
        return true;
    }

    bool push(T val)
    {
        if (full() == true)
        {
            throw std::out_of_range("The stack is full");
        }
        else
        {
            s[count] = val;
            count++;
        }
        return true;
    }
};
2.栈实现
#include <exception>
template <class T>
struct Node {
    Node *prep;
    Node *next;
    T data;
};



template <class T>
class Stack_by_Linked_List {
private:
    Node<T> *head;
public:
    Stack_by_Linked_List() :head(new Node<T>()) {
        head->prep = nullptr;
        head->next = nullptr;
        head->data = T();
    }
    ~Stack_by_Linked_List() {
        Node<T> *p, *pdel;
        p = head;
        while (p->next != nullptr)
        {
            pdel = p;
            p = p->next;
            delete pdel;
        }
        delete p;
    }

    Node<T>* last_element(){
        Node<T>* p = head;
        while (p->next != nullptr)
        {
            p = p->next;
        }
        return p;
    }

    bool push(T val){
        Node<T> *p, *pnew = new Node<T>;

        pnew->next = nullptr;
        pnew->data = val;

        p = last_element();
        pnew->prep = p;
        p->next = pnew;
        return true;
    }

    T top() {
        Node<T> *p;
        p = last_element();
        return p->data;
    }

    bool pop()
    {
        Node<T> *p = last_element();
        if (p==head)
        {
            throw std::out_of_range("Underflow");
        }
        else
        {
            p->prep->next = nullptr;
            delete p;
        }
        return true;
    }

    bool empty()
    {
        return (head->next == nullptr);
    }

    int size()
    {
        Node<T> *p=head;
        int count = 0;
        while (p->next != nullptr)
        {
            p = p->next;
            count++;
        }
        return count;
    }
};

2. 队列&优先队列

在队列中,被删去的总是在集合中存在时间最长的元素。 ——《算法导论》
即所谓“先进先出”

The priority_queue class orders its elements so that the largest element is always at the top position. It supports insertion of an element and the inspection and removal of the top element. --msdn
优先队列中,“某种排序方式下”的最大值将被放在队首的位置。此处的“最大值”仅对排序方式而言,可以是队列中元素大小的最小值,也可以是其它。

PS:优先队列的实现留坑

1.队列 数组实现
#include <exception>

template <class T, class SIZETYPE = int, SIZETYPE MAXSIZE=1000>
class Quene_By_Array {
private:
    T arr[MAXSIZE];
    SIZETYPE head;
    SIZETYPE tail;
    SIZETYPE count;
public:
    Quene_By_Array(): head(SIZETYPE()), tail(SIZETYPE()),count(SIZETYPE()){
        SIZETYPE i;
        for (i = SIZETYPE(); i < MAXSIZE; i++)
        {
            arr[i] = T();
        }
    }
    ~Quene_By_Array(){}

    SIZETYPE size()
    {
        return count;
    }
    
    bool empty()
    {
        if (size() == SIZETYPE())
        {
            return true;
        }
        else return false;
    }
    
    bool full()
    {
        if (size() == MAXSIZE)
        {
            return true;
        }
        else return false;
    }
    
    bool push(T val){
        
        if (full())
        {
            throw std::out_of_range("Overflow");
        }
        else
        {
            arr[tail] = val;
            count++;
            if (tail == MAXSIZE - 1)
            {
                tail = 0;
            }
            else
            {
                tail++;
            }
        }
        return true;
    }

    T front()
    {
        if (!empty())
            return arr[head];
        else
        {
            throw std::out_of_range("Underflow");
        }
    }

    bool pop() {
        if (empty())
        {
            throw std::out_of_range("Underflow");
        }
        else
        {
            if (head == MAXSIZE - 1)
            {
                head = 0;
            }
            else
            {
                head++;
            }
            count--;
        }
        return true;
    }
};
2.队列 链表实现
template <class T>
struct Node {
    Node* prep;
    Node* next;
    T data;
};

template<class T>
class Queue_By_Linked_List
{
private:
    Node<T> *head;

public:
    Queue_By_Linked_List() :head(new Node<T>()) {
        head->prep = nullptr;
        head->next = nullptr;
        head->data = T();
    }
    ~Queue_By_Linked_List() {
        Node<T> *p, *pdel;
        p = head;
        while (p->next != nullptr)
        {
            pdel = p;
            p = p->next;
            delete pdel;
        }
        delete p;
    }
    Node<T>* last_element() {
        Node<T>* p = head;
        while (p->next != nullptr)
        {
            p = p->next;
        }
        return p;
    }

    bool push(T val) {
        Node<T> *p, *pnew = new Node<T>;

        pnew->next = nullptr;
        pnew->data = val;

        p = last_element();
        pnew->prep = p;
        p->next = pnew;
        return true;
    }

    T front() {
        Node<T> *p;
        if (empty())
        {
            throw std::out_of_range("Underflow");
        }
        else
        {
            p = head->next;
            return p->data;
        }
    }

    bool pop()
    {
        Node<T> *p = nullptr;
        if (empty())
        {
            throw std::out_of_range("Underflow");
        }
        else
        {
            p=head->next;
            head->next = p->next;
            if (p->next!=nullptr) p->next->prep = head;
            delete p;
        }
        return true;
    }

    bool empty()
    {
        return (head->next == nullptr);
    }

    int size()
    {
        Node<T> *p = head;
        int count = 0;
        while (p->next != nullptr)
        {
            p = p->next;
            count++;
        }
        return count;
    }
};

3.<stack>头文件

(注:以下内容均参考msdn)

  • 语法

      template <class Type,class Container=deque <Type> >
      class stack
  • 成员函数
    • empty()

      判断栈是否为空

    • pop()

      从栈顶删除元素

    • push(const Type& val)

      向栈顶插入元素

    • size()

      返回栈中元素的数量

    • top()

      返回栈顶元素的reference

4.<queue>头文件

1)队列
  • 语法

      template <class Type, class Container = deque <Type>>  
      class queue
  • 成员函数
    • empty()

      判断队列是否为空

    • pop()

      从队首删除元素

    • push(const Type& val)

      向队尾插入元素

    • size()

      返回队列元素的数量

    • front()

      返回队首元素的reference

    • back()

      返回队尾元素的reference

2)优先队列
  • 语法

      template <class Type, class Container= vector <Type>, class Compare= less <typename Container ::value_type>>  
      class priority_queue
  • 成员函数

    • empty()

      判断优先队列是否为空

    • pop()

      从队首删除最大的元素

    • push(const Type& val)

      插入元素

    • size()

      返回优先队列元素的数量

    • top()

      返回队尾元素的const reference

  • 关于 Compare( 此处未参考msdn! )

    我知道可以使用方法有

    1. 使用less和great
    2. 重载小于号
    3. 写一个比较函数

5.<deque>头文件

双端队列,即限定插入和删除操作在两端进行。

  • 语法

      template <class Type, class Allocator =allocator<Type>>  
      class deque
  • 成员函数

    • push_back(const Type& val)

      从队尾插入元素

    • push_front(const Type& val)

      从队首插入元素

    • pop_front()

      从队首删除元素

    • pop_back()

      从队尾删除元素

    • empty()

      判断队列是否为空

    • begin()

      返回一个指向队首的iterator

    • end()

      返回一个指向队首的iterator

    • size()

      返回双端队列元素的数量

    • back()

      返回队尾元素的reference

后记 2017.05.22

因为怕来不及所以没有分离cpp和h 后面我会分离了传到github上的

转载于:https://www.cnblogs.com/circlek/p/6891200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值