Chain queue template

All data structures, algorithms and applications curricular templates please click: https://blog.csdn.net/weixin_44077863/article/details/101691360

Chain queue template as follows: (STL has various queue explain, remember to look after the template oh ~)

template<class T>
class LinkNode{
public:
	T data;
	LinkNode<T> *next;
    LinkNode():next(0){}
	LinkNode(const T &x,LinkNode<T> *p=0):data(x),next(p){}
};
template<class T>
class LinkQueue{
private:
	int size;//当前元素的个数 
	LinkNode<T> *front,*rear;//头尾元素指针 
public:
	LinkQueue():size(0),front(0),rear(0){}
	~LinkQueue(){Clear();}
	void Clear(){
		while(front){
			rear=front;
			front=front->next;
			delete rear;
		}
		rear=size=0;
	}
	bool EnQueue(const T item){
		if(!rear) front=rear=new LinkNode<T>(item);
		else{
			rear->next=new LinkNode<T>(item);
			rear=rear->next;
		}
		size++;
		return 1;
	}
	bool DeQueue(T &item){
		LinkNode<T> *tmp=front;
		if(!size){
			cout<<"队列为空!"<<endl;
			return 0;
		}
		item=front->data;
		front=front->next;
		delete tmp;
		if(!front) rear=0;
		size--;
		return 1;
	}
	bool GetFront(T &item){
		if(!size){
			cout<<"队列为空!"<<endl;
			return 0;
		}
		item=front->data;
		return 1;
	}
};

C ++ STL queue :( not speak a variety of special concrete, because there are many and list, similar to the stack section, Keguan can see previous blog blogger)

Common queue (few very simple operation, and no iterators)

#include <queue>
queue<int> q;
q.emplace(x);//入队
q.push(x);//入队
q.pop();//出队
q.front();//队首元素
q.back();//队尾元素
q.empty();
q.size();
q.swap(q2);
q.clear();

Deque deque (support iterators, begin end and other functions very simple not say)

Then although that is double-ended queue, to see how the function is double-ended vector. . .

#include <deque>
deque<int> q;//空
deque<int> q(n);//n个0
deque<int> q(n,x);//n个x
deque<int> q(q2);//拷贝构造
q.push_front(x);//入队首
q.emplace_front(x);//入队首
q.push_back(x);//入队尾
q.emplace_back(x);//入队尾
q.pop_front();//出队首
q.pop_back();//出队尾
q.empty();
q.size();
q.swap(q2);
q.clear();
q[i];//支持下标访问,屌啊
q.at(i);//和array一样,带try-catch查越界功能,除此外和q[i]一样
q.assign(it1,it2);
q.assign(n,x);//这俩和list差不多,就是个初始化
q.erase(it);
q.erase(it1,it2);//这俩和list差不多,就是个删除对应迭代器闭区间
//但是人家list还有remove,你没有。。
q.emplace(it,x);
q.insert(it,x);
q.insert(it,n,x);
q.insert(it,a,a+5);
q.insert(it,g.begin(),g.end());
//上面这些都是插入,和list差不多,所以也不多说了
q.max_size();//和list一样,无穷大,似乎只有array是指定最大空间的(不过毕竟array和a[]基本一样)
q.resize();//和list一样,当前大小的修改
q.shrink_to_fit();//听说是什么,将容量设置为容器长度
//不是很懂,为什么会有多余空间产生呢,不清楚
//正在写此博文的博主刚上大二,实在不是很懂这个,还请客官自己研究

:( priority queue is very important, more content will then send a blog post, it reproduced here)

Priority Queue: https://blog.csdn.net/weixin_44077863/article/details/102020177

Published 49 original articles · won praise 0 · Views 1722

Guess you like

Origin blog.csdn.net/weixin_44077863/article/details/102000902