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

智慧团建pc版官网优化大师专业版

智慧团建pc版官网,优化大师专业版,vps网站目录显示灰色的,多语种网站数据结构第三篇 一、几个注意点 1、同时持有头尾结点的引用 双链表一般同时持有头尾结点的引用 因为在工程应用中,通常在容器尾插入元素,双链表持有尾部节点的引用,就可以在O(1)时间复杂度的情况下在尾部添加元素。…

数据结构第三篇

一、几个注意点

1、同时持有头尾结点的引用

双链表一般同时持有头尾结点的引用

因为在工程应用中,通常在容器尾插入元素,双链表持有尾部节点的引用,就可以在O(1)时间复杂度的情况下在尾部添加元素。

对于单链表,持有尾节点的引用也有优化效果,比如在单链表尾部添加元素;如果没有尾部节点的引用,就需要遍历整个链表找到尾部节点,时间复杂度O(n);如果有尾部节点的引用,就可以在O(1)的时间复杂度内完成尾部添加元素的操作;如果删除一次单链表的尾节点,那么之前的尾节点的引用就会失效,还需要遍历一次才能找到尾节点?但是其实,可以更新尾结点的引用

2、虚拟头尾结点

在创建双链表时,创建一个虚拟的头尾结点,无论双链表是否为空,这两个结点都存在,这样就不会出现空指针的问题,避免边界处理的问题。

空的双链表:

dummyHead <-> dummyTail

添加两个元素:

dummyHead <-> 1 <-> 2 <-> dummyTail

之前需要在头部插入元素,需要分类讨论,现在有了虚拟头尾结点,无论是否为空,都不需要分类讨论。

注意:虚拟头尾结点是对外不可见的

3、内存泄漏

// 假设单链表头结点 head = 1 -> 2 -> 3 -> 4 -> 5
// 删除单链表头结点
head = head.next;  // 此时 head = 2 -> 3 -> 4 -> 5

删除结点,最好是把删除的结点都设为null

二、代码实现

1、双链表
#include <iostream>
#include <stdexcept>template<typename E>
class MyLinkedList {// 虚拟头尾节点struct Node {E val;Node* next;Node* prev;Node(E value) : val(value), next(nullptr), prev(nullptr) {}};Node* head;Node* tail;int size;public:// 构造函数初始化虚拟头尾节点MyLinkedList() {head = new Node(E());tail = new Node(E());head->next = tail;tail->prev = head;size = 0;}// ***** 增 *****void addLast(E e) {Node* x = new Node(e);Node* temp = tail->prev;// temp <-> xtemp->next = x;x->prev = temp;x->next = tail;tail->prev = x;// temp <-> x <-> tailsize++;}void addFirst(E e) {Node* x = new Node(e);Node* temp = head->next;// head <-> temptemp->prev = x;x->next = temp;head->next = x;x->prev = head;// head <-> x <-> tempsize++;}void add(int index, E element) {checkPositionIndex(index);if (index == size) {addLast(element);return;}// 找到 index 对应的 NodeNode* p = getNode(index);Node* temp = p->prev;// temp <-> p// 新要插入的 NodeNode* x = new Node(element);p->prev = x;temp->next = x;x->prev = temp;x->next = p;// temp <-> x <-> psize++;}// ***** 删 *****E removeFirst() {if (size < 1) {throw std::out_of_range("No elements to remove");}// 虚拟节点的存在是我们不用考虑空指针的问题Node* x = head->next;Node* temp = x->next;// head <-> x <-> temphead->next = temp;temp->prev = head;E val = x->val;delete x;// head <-> tempsize--;return val;}E removeLast() {if (size < 1) {throw std::out_of_range("No elements to remove");}Node* x = tail->prev;Node* temp = tail->prev->prev;// temp <-> x <-> tailtail->prev = temp;temp->next = tail;E val = x->val;x->prev = null;x->next = null;delete x;// temp <-> tailsize--;return val;}E remove(int index) {checkElementIndex(index);// 找到 index 对应的 NodeNode* x = getNode(index);Node* prev = x->prev;Node* next = x->next;// prev <-> x <-> nextprev->next = next;next->prev = prev;E val = x->val;x->prev = null;x->next = null;delete x;// prev <-> nextsize--;return val;}// ***** 查 *****E get(int index) {checkElementIndex(index);// 找到 index 对应的 NodeNode* p = getNode(index);return p->val;}E getFirst() {if (size < 1) {throw std::out_of_range("No elements in the list");}return head->next->val;}E getLast() {if (size < 1) {throw std::out_of_range("No elements in the list");}return tail->prev->val;}// ***** 改 *****E set(int index, E val) {checkElementIndex(index);// 找到 index 对应的 NodeNode* p = getNode(index);E oldVal = p->val;p->val = val;return oldVal;}// ***** 其他工具函数 *****int getSize() const {return size;}bool isEmpty() const {return size == 0;}void display() {std::cout << "size = " << size << std::endl;for (Node* p = head->next; p != tail; p = p->next) {std::cout << p->val << " <-> ";}std::cout << "null" << std::endl;std::cout << std::endl;}private:Node* getNode(int index) {checkElementIndex(index);Node* p = head->next;// TODO: 可以优化,通过 index 判断从 head 还是 tail 开始遍历for (int i = 0; i < index; i++) {p = p->next;}return p;}bool isElementIndex(int index) const {return index >= 0 && index < size;}bool isPositionIndex(int index) const {return index >= 0 && index <= size;}// 检查 index 索引位置是否可以存在元素void checkElementIndex(int index) const {if (!isElementIndex(index))throw std::out_of_range("Index: " + std::to_string(index) + ", Size: " + std::to_string(size));}// 检查 index 索引位置是否可以添加元素void checkPositionIndex(int index) const {if (!isPositionIndex(index))throw std::out_of_range("Index: " + std::to_string(index) + ", Size: " + std::to_string(size));}~MyLinkedList() {while (size > 0) {removeFirst();}delete head;delete tail;}
};int main() {MyLinkedList<int> list;list.addLast(1);list.addLast(2);list.addLast(3);list.addFirst(0);list.add(2, 100);list.display();// size = 5// 0 <-> 1 <-> 100 <-> 2 <-> 3 <-> nullreturn 0;
}
2、单链表
#include <iostream>
#include <stdexcept>template <typename E>
class MyLinkedList2 {
private:// 节点结构struct Node {E val;Node* next;Node(E value) : val(value), next(nullptr) {}};Node* head;// 实际的尾部节点引用Node* tail;int size_;public:MyLinkedList2() {head = new Node(E());tail = head;size_ = 0;}void addFirst(E e) {Node* newNode = new Node(e);newNode->next = head->next;head->next = newNode;if (size_ == 0) {tail = newNode;}size_++;}void addLast(E e) {Node* newNode = new Node(e);tail->next = newNode;tail = newNode;size_++;}void add(int index, E element) {checkPositionIndex(index);if (index == size_) {addLast(element);return;}Node* prev = head;for (int i = 0; i < index; i++) {prev = prev->next;}Node* newNode = new Node(element);newNode->next = prev->next;prev->next = newNode;size_++;}E removeFirst() {if (isEmpty()) {throw std::out_of_range("No elements to remove");}Node* first = head->next;head->next = first->next;if (size_ == 1) {tail = head;}size_--;E val = first->val;delete first;return val;}E removeLast() {if (isEmpty()) {throw std::out_of_range("No elements to remove");}Node* prev = head;while (prev->next != tail) {prev = prev->next;}E val = tail->val;delete tail;prev->next = nullptr;tail = prev;size_--;return val;}E remove(int index) {checkElementIndex(index);Node* prev = head;for (int i = 0; i < index; i++) {prev = prev->next;}Node* nodeToRemove = prev->next;prev->next = nodeToRemove->next;// 删除的是最后一个元素if (index == size_ - 1) {tail = prev;}size_--;E val = nodeToRemove->val;delete nodeToRemove;return val;}// ***** 查 *****E getFirst() {if (isEmpty()) {throw std::out_of_range("No elements in the list");}return head->next->val;}E getLast() {if (isEmpty()) {throw std::out_of_range("No elements in the list");}return getNode(size_ - 1)->val;}E get(int index) {checkElementIndex(index);Node* p = getNode(index);return p->val;}// ***** 改 *****E set(int index, E element) {checkElementIndex(index);Node* p = getNode(index);E oldVal = p->val;p->val = element;return oldVal;}// ***** 其他工具函数 *****int size() {return size_;}bool isEmpty() {return size_ == 0;}~MyLinkedList2() {Node* current = head;while (current != nullptr) {Node* next = current->next;delete current;current = next;}}private:bool isElementIndex(int index) {return index >= 0 && index < size_;}bool isPositionIndex(int index) {return index >= 0 && index <= size_;}// 检查 index 索引位置是否可以存在元素void checkElementIndex(int index) {if (!isElementIndex(index)) {throw std::out_of_range("Index: " + std::to_string(index) + ", size_: " + std::to_string(size_));}}// 检查 index 索引位置是否可以添加元素void checkPositionIndex(int index) {if (!isPositionIndex(index)) {throw std::out_of_range("Index: " + std::to_string(index) + ", size_: " + std::to_string(size_));}}// 返回 index 对应的 Node// 注意:请保证传入的 index 是合法的Node* getNode(int index) {Node* p = head->next;for (int i = 0; i < index; i++) {p = p->next;}return p;}
};int main() {MyLinkedList2<int> list;list.addFirst(1);list.addFirst(2);list.addLast(3);list.addLast(4);list.add(2, 5);std::cout << list.removeFirst() << std::endl; // 2std::cout << list.removeLast() << std::endl; // 4std::cout << list.remove(1) << std::endl; // 5std::cout << list.getFirst() << std::endl; // 1std::cout << list.getLast() << std::endl; // 3std::cout << list.get(1) << std::endl; // 3return 0;
}


文章转载自:

http://1zPctEWR.ptdzm.cn
http://iCzHV0Th.ptdzm.cn
http://g76wtgHB.ptdzm.cn
http://fNSgWnXF.ptdzm.cn
http://qQtPdl6l.ptdzm.cn
http://KEqDVzC3.ptdzm.cn
http://tdyD3rhf.ptdzm.cn
http://lDhomoym.ptdzm.cn
http://0K3JrIIZ.ptdzm.cn
http://CDVubimR.ptdzm.cn
http://SbdGEteR.ptdzm.cn
http://JPPiEasW.ptdzm.cn
http://dP477oH0.ptdzm.cn
http://Jvb8AEKf.ptdzm.cn
http://QlhLE1it.ptdzm.cn
http://Z0l48SrR.ptdzm.cn
http://F2fXgxiV.ptdzm.cn
http://HrvI7VaZ.ptdzm.cn
http://6ULihX8j.ptdzm.cn
http://AUuLNlDt.ptdzm.cn
http://o5dxGXhy.ptdzm.cn
http://F1hqcbA4.ptdzm.cn
http://ShnzUrGM.ptdzm.cn
http://IiUVNKWt.ptdzm.cn
http://0av58StH.ptdzm.cn
http://IUt3i2ww.ptdzm.cn
http://6fr6UeHD.ptdzm.cn
http://IEdS02cn.ptdzm.cn
http://snXVpJt1.ptdzm.cn
http://3Gm7daD8.ptdzm.cn
http://www.dtcms.com/wzjs/755194.html

相关文章:

  • sae 企业网站通州做网站
  • 组建一个网站网站整合营销
  • 网站图片尺寸大小wordpress主题 插件
  • 中邮通建设咨询有限公司官方网站长沙电商优化
  • 长沙 网站建设凡科网站建设平台好么
  • 网站推荐男生正能量2021seo排名软件价格
  • 手机网站搭建多少钱做外贸的人经常逛的网站
  • 彩票网站做一级代理犯法吗wordpress手机评论
  • 上门做网站公司哪家好郑州做网站推
  • 怎么做试玩平台推广网站淄博市建设档案馆网站
  • 上海建设工程造价信息网站广西企业网站建设
  • 四川建设人才考试网官方网站密云重庆网站建设
  • 线上推广宣传方式有哪些太原百度快速优化
  • 济南天桥区网站建设为什么网站权重会掉
  • 通州网站建设多少钱巫山那家做网站厉害
  • 播州区住房和城乡建设局网站网站cms识别
  • 百度站长论坛金融网站模板免费下载
  • 网站建站网站80s隐秘而伟大建网站过程
  • o2o平台都有哪些网站公司小程序公众平台官网
  • 自贡建设局网站建网站 免费
  • 江苏省建设考试信息管理系统网站深圳展览设计公司
  • 网站建设中怎样设置背景商品房交易网
  • 个人网站命名的要求徐闻手机网站建设公司
  • 关于建设门户网站的请示wordpress同步发帖
  • 用别的公司域名做网站红酒 公司 网站建设
  • 东莞英文网站制作软件技术大专出来都去干嘛了
  • 温州网站建站网站建设先进个人自荐
  • 超炫网站欣赏wordpress 手机 看视频
  • 怎么夸一个网站开发公司那种网站怎么搜关键词
  • 奢做品二手 哪个网站好北京网站设计 培训学校