zoukankan      html  css  js  c++  java
  • 用首位相接的数组仿stl双端队列deque

    这是stl deque的测试 仅实现部分功能
    在这里插入图片描述
    这是利用首位相接的数组 [循环队列]仿stl双端队列deque
    在这里插入图片描述
    将得到一样的结果
    在这里插入图片描述

    #include<iostream>
    #include<unordered_map>
    using namespace std;
    template<class T>
    class Deque
    {
    	int start, rear;
    	T* elements;
    	int maxsize ;
    public:
    	Deque();
    	~Deque() { delete[]elements; }
    	bool empty();
    	bool isfull();
    	void clear();
    	int front();
    	int back();
    	void push_back(int x);
    	void push_front(int x);
    	void pop_back();
    	void pop_front();
    	int at(int x);
    	T operator[](int x);
    };
    template<class T>
    Deque<T>::Deque()
    {
    	maxsize = 100;
    	elements = new T[maxsize];
    	start = rear = 0;
    }
    template<class T>
    bool Deque<T>::empty()
    {
    	if (start == rear)
    		return true;
    	return false;
    }
    template<class T>
    bool Deque<T>::isfull()
    {
    	if ((rear+1)%maxsize== start)
    		return true;
    	return false;
    }
    template<class T>
    void Deque<T>::clear()
    {
    	start = rear = 0;
    }
    template<class T>
    int Deque<T>::front()
    {
    	if (!empty())
    		return elements[start];
    	else
    		cout << "队列为空" << endl;
    }
    template<class T>
    int Deque<T>::back()
    {
    	if (!empty())
    	{
    		int temp = (rear - 1 + maxsize) % maxsize;
    		return elements[temp];
    	}
    	else
    		cout << "队列为空" << endl;
    }
    template<class T>
    void Deque<T>::push_back(int x)
    {
    	if (!isfull())
    	{
    		elements[rear] = x;
    		rear=(rear+1+maxsize)%maxsize;
    	}
    	else
    		cout << "队列已满,不能从尾部插入" << endl;
    }
    template<class T>
    void Deque<T>::push_front(int x)
    {
    	if (isfull())
    		cout << "队列已满,不能从头部插入" << endl;
    	else {
    		start = (start - 1 + maxsize) % maxsize;
    		elements[start] = x;
    	}
    
    }
    template<class T>
    void Deque<T>::pop_back()
    {
    	if (empty())
    		cout << "队列为空,不能删除" << endl;
    	else
    	{
    		rear = (rear - 1 + maxsize) % maxsize;
    	}
    }
    template<class T>
    void Deque<T>::pop_front()
    {
    	if (empty())
    		cout << "队列为空,不能删除" << endl;
    	else
    	{
    		start = (start + 1 + maxsize) % maxsize;
    	}
    }
    template<class T>
    int Deque<T>::at(int x)
    {
    	int r = (start + x + maxsize) % maxsize;
    	return elements[r];
    }
    template<class T>
    T Deque<T>::operator[](int x)
    {
    	return elements[(start + x+maxsize)%maxsize];
    }
    int main()
    {
    	Deque<int>q;
    	q.push_back(3);
    	q.push_back(4);
    	q.push_back(5);
    	q.push_back(6);
    	q.push_front(7);
    	cout << q.front() << endl;
    	cout << q.back() << endl;
    	cout << q.at(3) << endl;
    	cout << q[0] << endl;
    	q.empty();
    	q.pop_back();
    	q.pop_front();
    	cout << q.front() << endl;
    	cout << q.back() << endl;
    }
    
    
  • 相关阅读:
    《ASP.NET1200例》实现投票的用户控件
    《转》这些年这些感悟
    《转》不要过打折的生活,当你发现这些你有了,说明你开始成熟了
    HTML控件ID和NAME属性及在CS页面获得.ASPX页面中HTML控件的值
    逻辑回归(1)
    MySQL笔记5-----索引(覆盖索引等)
    MySQL笔记4------面试问题
    MySQL-----笔记3:存储引擎
    Python可视化数据------seaborn
    树(2)-----leetcode(层、深度、节点)
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811925.html
Copyright © 2011-2022 走看看