博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己写deque
阅读量:6049 次
发布时间:2019-06-20

本文共 2536 字,大约阅读时间需要 8 分钟。

//deque/*what is a deque?In Chinese, it's called "双端队列".It's different from a queue.Its elements can be added to or removed from either the front(head) or back(tail) ,called a head-tail linked list.输入限制dequeAn input-restricted deque is one where deletion can be made from both ends, but insertion can be made at one end only.输出限制dequeAn output-restricted deque is one where insertion can be made at both ends, but deletion can be made from one end onlyQueue and stack can be considered spectalizations of deques.There are at least two common ways to efficiently implement a deque: with a modified dynamic arry or with a doubly linked listC++dq1.push_back(x)    insert element at backdq1.push_front(x)    insert element at frontdq1.pop_back(x)      remove last elementdq1.pop_front(x)      remove first elementdq1.back()              return last elementdq1.front()              return first element*///自己写deque //double linked list版template
class Dequeue{private: struct Node { Object data; Node* next; Node* prev; Node(const Object& d = Object(), Node *p = NULL, Node *n = NULL) :data(d), next(n), prev(p){} };public: Dequeue() { init(); } Dequeue(const Dequeue& q) { init(); *this = q; } const Dequeue& operator=(const Dequeue& q) { if (this == &q) return *this; clear(); Node *p = q.head->next; while (p->next != q.tail) { this.push_back(p->data); p = p->next; } return *this; } ~Dequeue() { clear(); delete head; delete tail; } //判空 bool isEmpty() { return size == 0; } //push_back void push_back(Object item) { size++; Node* p = new Node; p->data = item; Node* q = tail->prev; p->next = tail; p->prev = q; q->next = p; tail->prev = p; } //push_front void push_front(Object item) { size++; Node* p = new Node; p->data = item; Node* q = head->next; p->next = q; p->prev = head; head->next = p; q->prev = p; } //pop_back void pop_back() { size--; Node*p = tail->prev; Node*q = p->prev; q->next = tail; tail->prev = q; delete p; } //pop_front void pop_front() { size--; Node *p = head->next; Node *q = p->next; head->next = q; q->prev = head; delete p; } //back Object back() { return tail->prev->data; } //front Object front() { return head->next->data; } //clear void clear() { while (!isEmpty()) { pop_back(); } }//getsize int getSize() { return size; }private: Node* head; Node* tail; int size; void init() { head = new Node; tail = new Node; size = 0; head->next = tail; tail->prev = head; }};

  

转载于:https://www.cnblogs.com/KennyRom/p/5967550.html

你可能感兴趣的文章
用wxpython制作可以用于 特征筛选gui程序
查看>>
【转载】 [你必须知道的.NET]目录导航
查看>>
数据存储小例
查看>>
C++中构造函数详解
查看>>
电商网站中添加商品到购物车功能模块2017.12.8
查看>>
android 模拟器 hardWare 属性说明
查看>>
六款值得推荐的android(安卓)开源框架简介
查看>>
max_element( )
查看>>
java中的类
查看>>
pthread_create线程创建的过程剖析(转)
查看>>
android存储访问框架Storage Access Framework
查看>>
Mysql C API调用存储过程的总结
查看>>
Oracle的层次查询
查看>>
远程调用服务(RPC)和消息(Message Queue)对比及其适用/不适用场合
查看>>
FreeBSD 的 Ports 系统
查看>>
有关web
查看>>
读Nginx官方文档笔记
查看>>
Spring中用了哪些设计模式?
查看>>
存储问题
查看>>
转: jquery中ajax回调函数使用this
查看>>