拓冰建站拓冰建站
首页 / 资讯中心 / 正文

算法9.链式队列

算法9.链式队列// 09_链式队列.cpp : 此文件包含 main 函数。程序执行将在此处开始并结束。//#includeiostreamusingnamespacestd;// 链式队列classLinkQueue{public:LinkQueue(){head_newNode();head_-next_head_;head_-pre_head_;}~LinkQueue(){Node*phead_-next_;while(p!head_){head_-next_p-next_;p-next_-pre_head_;deletep;phead_-next_;}deletehead_;head_nullptr;}public:// 入队voidpush(intval){Node*nodenewNode(val);node-next_head_;node-pre_head_-pre_;head_-pre_-next_node;head_-pre_node;}// 出队voidpop(){Node*phead_-next_;head_-next_p-next_;p-next_-pre_head_;deletep;}// 获取队头元素intfront()const{if(head_-next_head_){throwqueue is empty!;}returnhead_-next_-data_;}// 获取队尾元素intback()const{if(head_-next_head_){throwqueue is empty!;}returnhead_-pre_-data_;}// 判空boolempty()const{returnhead_-next_head_;}private:structNode{Node(intdata0):data_(data),next_(nullptr),pre_(nullptr){}intdata_;Node*next_;Node*pre_;};Node*head_;// 指向头节点};intmain(){intarr[]{12,4,56,7,89,31,53,75};LinkQueue que;for(intv:arr){que.push(v);}coutque.front()endl;coutque.back()endl;que.push(100);que.push(200);que.push(300);coutque.front()endl;coutque.back()endl;while(!que.empty()){coutque.front() que.back()endl;que.pop();}}
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门