智能网站建设公司排名wordpress标签图片

当前位置: 首页 > news >正文

智能网站建设公司排名,wordpress标签图片,高质量的南昌网站建设,在家可以加工的小工厂概述 队列#xff0c;是一种基本的数据结构#xff0c;也是一种数据适配器。它在底层上以链表方法实现。 队列的显著特点是他的添加元素与删除元素操作#xff1a;先加入的元素总是被先弹出。 一个队列应该应该是这样的#xff1a; ————–QUEUE————-——…概述 队列是一种基本的数据结构也是一种数据适配器。它在底层上以链表方法实现。 队列的显著特点是他的添加元素与删除元素操作先加入的元素总是被先弹出。 一个队列应该应该是这样的 ————–QUEUE————-———— ———— ———— ———— pop() ←– T1 ←– T2 ←– T3 ←– T4 ←– push()———— ———— ———— ———— ——————————–front() back()注意——————————-→队列的内部是一张由front指向back的链表 Tn代表该元素被加入到队列的次序。 一个队列有以下四种基本行为 front()表示对队列头元素的访问操作。如得到元素T1。 pop()表示对队列头元素的弹出操作。我们弹出T1 ———QUEUE——————— ———— ————pop() ←– T2 ←– T3 ←– T4 ←– push()———— ———— ———— ———————–front() back() 现在T2成为队头元素。  back()表示对队列尾元素的访问操作。如当前会得到T4。 push()表示对队列尾部压入新元素。我们压入T5 ————–QUEUE————-———— ———— ———— ———— pop() ←– T2 ←– T3 ←– T4 ←– T5 ←– push()———— ———— ———— ———— ——————————–front() back() 现在T5成为尾元素。  接下来我们通过封装queue类,实现队列的一些基本功能 。Code和测试案例附后 命名空间 C有自己的std命名空间下的queue为了进行区分封装一个自己的动态数组命名空间custom_queue。 使用namespace关键字封装使用时可以声明using namespace custom_queu;在作用域内声明或custom_queue::局部声明。 namespace custom_queue{… }//作用域内声明 using namespace custom_queue;//局部声明 custom_queue::… 成员变量 template typename T是泛型作为数据适配器他的数据单位应该是任意一种类型此时暂用T表示至于T为何物将在实例化时以告知。 定义class类queue封装三个成员变量queue_nodeT* val_front; queue_nodeT* val_back; size_t val_size; size_t 是C/C标准在stddef.h中定义的这个头文件通常不需要#includesize_t 类型专门用于表示长度它是无符号整数。 我们还要额外定义嵌套类queue_node它只能被queue类使用这就实现了结构功能的封装。 queue_nodeT* val_front是队头处的指针。 queue_nodeT* val_back是队尾处的指针。 size_t val_size用于记录当前队列的长度。 templatetypename T class queue { private:templatetypename Tclass queue_node {private:…};queue_nodeT* val_front;queue_nodeT* val_back;size_t val_size; public:… } 定义class类queue_node封装两个成员变量T val; queue_node*next 声明友员类friend class queuqueue为模版类模版友员类前加泛型声明这使得queue可以操控queue_node的私有成员将queue_node的构造函数和析构函数定为私有这样就只用queue能管理queue_node了。 T val当前节点值。 queue_nodenext指向下一个节点 另有构造函数接受一个T elem创建新节点。 析构函数无须函数体完全由trie类代管略去不表。 禁用拷贝构造和重载等于号默认拷贝构造和等于号进行指针变量赋值这存在极大问题两指针争抢堆上的数据同一块数据另有深层拷贝解决略去不表。 templatetypename T class queue_node { private:templatetypename Tfriend class queue;T val;queue_node next;queue_node(T elem) :val(elem), next(nullptr) {};~queue_node(){};queue_node(const queue_node another)delete;queue_node operator(const queue_node another) delete; }; 创建销毁 提供四种构造。 无参构造创建空队列。 复制构造用另一个队列进行深度拷贝。所谓深度拷贝就是以another指针指向的值作为参数创建新指针而不是让两指针指向同一值。让队头获得创新得到的第一个节点然后以两个临时指针another_val与this_val进行同步this_val时时构造与another_val指向的值相同的新节点。 最后队尾获得创建得到的最后一个节点。 析构函数当队列非空循环进行头结点弹出。后面实现判断空队列行为和弹出行为。 另有重载等于号作用于复制构造相同。 queue() :val_front(nullptr), val_back(nullptr), val_size(0) {}; queue(const queue another) :queue() {int len another.val_size;val_size len;if (len) {queue_nodeT* this_valnew queue_nodeT(another.val_front-val);const queue_nodeT* another_val another.val_front-next;val_front this_val;while (–len) {this_val-next new queue_nodeT(another_val-val);this_val this_val-next;another_val another_val-next;}val_back this_val;} } ~queue() {while (!empty())pop(); } queue operator(const queue another){val_front val_back nullptr;int len another.val_size;val_size len;if (len) {queue_nodeT* this_val new queue_nodeT(another.val_front-val);const queue_nodeT* another_val another.val_front-next;val_front this_val;while (–len) {this_val-next new queue_nodeT(another_val-val);this_val this_val-next;another_val another_val-next;}val_back this_val;}return this; } 数据控制 获取长度返回val_size。 判断为空返回val_size ? false : true。 队尾压入如果是空队列队头申请新节点node后令队尾等于队头。否则在队尾后面申请新节点。 队头弹出如果是空队列抛出异常。否则获取当前头结点的next删除头节点后将next作为头结点。如果队列大小为1那么删除后应将头尾全部置为nullptr空节点。 size_t size() {return val_size; } bool empty() {return val_size ? false : true; } void push(const T elem) {if (val_size 0) {val_front new queue_nodeT(elem);val_back val_front;}else {val_back-next new queue_nodeT(elem);val_back val_back-next;}val_size; } void pop(){assert(val_size 0);queue_nodeT temp val_front-next;delete val_front;val_front temp;val_size–;if (!val_size)val_front val_back nullptr; } 数据访问 访问队头判断无异常后返回队头的常量引用。 访问队尾判断无异常后返回队尾的常量引用。 我们的queue访问操作不支持接受方进行数据更改。 const T front() {assert(val_size 0);return (val_front-val); } const T back() {assert(val_size 0);return (val_back-val); } Code #pragma once #include cassert namespace custom_queue {templatetypename Tclass queue {private:templatetypename Tclass queue_node {private:templatetypename Tfriend class queue;T val;queue_node* next;queue_node(T elem) :val(elem), next(nullptr) {};~queue_node(){};queue_node(const queue_node another)delete;queue_node operator(const queue_node another) delete;};queue_nodeT* val_front;queue_nodeT* val_back;size_t val_size;public:queue() :val_front(nullptr), val_back(nullptr), val_size(0) {};queue(const queue another) :queue() {int len another.val_size;val_size len;if (len) {queue_nodeT* this_valnew queue_nodeT(another.val_front-val);const queue_nodeT* another_val another.val_front-next;val_front this_val;while (–len) {this_val-next new queue_nodeT(another_val-val);this_val this_val-next;another_val another_val-next;}val_back this_val;}}~queue() {while (!empty())pop();}queue operator(const queue another){val_front val_back nullptr;int len another.val_size;val_size len;if (len) {queue_nodeT* this_val new queue_nodeT(another.val_front-val);const queue_nodeT* another_val another.val_front-next;val_front this_val;while (–len) {this_val-next new queue_nodeT(another_val-val);this_val this_val-next;another_val another_val-next;}val_back this_val;}return this;}int size() {return val_size;}bool empty() {return val_size ? false : true;}void push(const T elem) {if (val_size 0) {val_front new queue_nodeT(elem);val_back val_front;}else {val_back-next new queue_nodeT(elem);val_back val_back-next;}val_size;}void pop(){assert(val_size 0);queue_nodeT temp val_front-next;delete val_front;val_front temp;val_size–;if (!val_size)val_front val_back nullptr;}const T front() {assert(val_size 0);return (val_front-val);}const T back() {assert(val_size 0);return (val_back-val);}}; } 测试 #include iostream #include queue.h using namespace std; int main() {custom_queue::queuecharque1;que1.push(a); que1.push(b); que1.push©;custom_queue::queuecharque2(que1);while (!que1.empty()) {cout que1.front();que1.pop();}cout endl;while (!que2.empty()) {cout que2.front();que2.pop();}cout endl;que2.push(x);cout que2.front() que2.back();cout endl;return 0; }