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

门户网站建站要求b2b b2c c2c o2o区别

门户网站建站要求,b2b b2c c2c o2o区别,深圳有什么做招聘网站的公司吗,做网站赚钱交税前言 前面有很详细的讲过线性表(顺序表和链表),当时讲的链表以单链表为主,但在实际应用中双链表有很多应用场景,例如大家熟知的LinkedList。 双链表与单链表区别 单链表和双链表都是线性表的链式实现,它们的主要区别在于节点结构…

前言

前面有很详细的讲过线性表(顺序表和链表),当时讲的链表以单链表为主,但在实际应用中双链表有很多应用场景,例如大家熟知的LinkedList。

image-20231031232421766

双链表与单链表区别

单链表和双链表都是线性表的链式实现,它们的主要区别在于节点结构。单链表的节点包含数据字段 data 和一个指向下一个节点的指针 next,而双链表的节点除了 datanext,还包含指向前一个节点的指针 pre。这个区别会导致它们在操作上有些差异。

单链表:

单链表的一个节点,有储存数据的data,还有后驱节点next(指针)。单链表想要遍历的操作都得从前节点—>后节点

image-20231031233306475

双链表:

双链表的一个节点,有存储数据的data,也有后驱节点next(指针),这和单链表是一样的,但它还有一个前驱节点pre(指针)。

image-20231031233635681

双链表结构的设计

上一篇讲单链表的时候,当时设计一个带头结点的链表就错过了不带头结点操作方式,这里双链表就不带头结点设计实现。所以本文构造的这个双链表是:不带头节点、带尾指针(tail)的双向链表。

对于链表主体:

public class DoubleLinkedList<T> {private Node<T> head;private Node<T> tail;private int size;public DoubleLinkedList(){this.head = null;this.tail = null;size = 0;}public void addHead(T data){}public void add(T data, int index){}public void addTail(T data){}public void deleteHead(){}public void delete(int index){}public void deleteTail(int index){}public T get(int index){}public int getSize() {return size;}private static class Node<T> {T data;Node<T> pre;Node<T> next;public Node() {}public Node(T data) {this.data = data;}}
}

具体操作分析

对于一个链表主要的操作还是增删,查询的话不做详细解释。

剖析增删其实可以发现大概有头插入、编号插入、末尾插入、头删除、编号删除、尾删除几种情况。然而这几种关于头尾操作的可能会遇到临界点比如链表为空时插入删除、或者删除节点链表为空。

这个操作是不带头结点的操作,所以复杂性会高一些!

头插入

头插入区分头为空和头不为空两种情况

头为空:这种情况head和tail都指向新节点

头不为空:

  1. 新节点的next指向head
  2. head的pre指向新节点
  3. head指向新节点(认新节点为head)

image-20231101232855888

尾插入

尾插需要考虑tail为null和不为null的情况。流程和头插类似,需要考虑tail指针最后的指向。

tail为null:此时head也为null,head和tail指向新节点。

tail不为null:

  • 新节点的pre指向tail
  • tail的next指向新节点
  • tail指向新节点
编号插入

按编号插入分情况讨论,如果是头插或者尾插就直接调用对应的方法。普通方法的实现方式比较灵活,可以找到前驱节点和后驱节点,然后进行指针插入,但是往往很多时候只用一个节点完成表示和相关操作,就非常考验对表示的理解,这里假设只找到preNode节点。
index为0:调用头插

index为size:调用尾插

index在(0,size):

  1. 找到前驱节点preNode
  2. 新节点next指向nextNode(此时用preNode.next表示)
  3. nextNode(此时新节点.next和preNode.next都可表示)的pre指向新节点
  4. preNode的next指向新节点
  5. 新节点的pre指向preNode

image-20231102000134083

头删除

头删除需要注意的就是删除不为空时候头删除只和head节点有关

head不为null:

  1. head = head.next 表示头指针指向下一个节点
  2. head 如果不为null(有可能就一个节点),head.pre = null 断掉与前一个节点联系 ;head如果为null,说明之前就一个节点head和pre都指向第一个节点,此时需要设置tail为null。

image-20231102002747786

尾删除

尾删除和头删除类似,考虑好tail节点情况

如果tail不为null:

  1. tail = tail.pre
  2. 如果tail不为null,那么tail.next = null 表示删除最后一个,如果tail为null,说明之前head和tail都指向一个唯一节点,这时候需要head = null。
编号删除

编号删除和编号插入类似,先考虑是否为头尾操作,然后再进行正常操作。

index为0:调用头删

index为size:调用尾删

index在(0,size):

  1. 找到待删除节点current
  2. 前驱节点(current.pre)的next指向后驱节点(current.next)
  3. 后驱节点的pre指向前驱节点

image-20231102075437513

完整代码

根据上面的流程,实现一个不带头结点的双链表,在查找方面,可以根据靠头近还是尾近,选择从头或者尾开始遍历。

代码:

/** 不带头节点的*/
package code.linearStructure;/*** @date 2023.11.02* @author bigsai* @param <T>*/
public class DoubleLinkedList<T> {private Node<T> head;private Node<T> tail;private int size;public DoubleLinkedList() {this.head = null;this.tail = null;size = 0;}// 在链表头部添加元素public void addHead(T data) {Node<T> newNode = new Node<>(data);if (head == null) {head = newNode;tail = newNode;} else {newNode.next = head;head.pre = newNode;head = newNode;}size++;}// 在指定位置插入元素public void add(T data, int index) {if (index < 0 || index > size) {throw new IndexOutOfBoundsException("Index is out of bounds");}if (index == 0) {addHead(data);} else if (index == size) {addTail(data);} else {Node<T> newNode = new Node<>(data);Node<T> preNode = getNode(index-1);//step 1 2 新节点与后驱节点建立联系newNode.next = preNode;preNode.next.pre = newNode;//step 3 4 新节点与前驱节点建立联系preNode.next = newNode;newNode.pre = preNode;size++;}}// 在链表尾部添加元素public void addTail(T data) {Node<T> newNode = new Node<>(data);if (tail == null) {head = newNode;tail = newNode;} else {newNode.pre = tail;tail.next = newNode;tail = newNode;}size++;}// 删除头部元素public void deleteHead() {if (head != null) {head = head.next;if (head != null) {head.pre = null;} else { //此时说明之前head和tail都指向唯一节点,链表删除之后head和tail都应该指向nulltail = null;}size--;}}// 删除指定位置的元素public void delete(int index) {if (index < 0 || index >= size) {throw new IndexOutOfBoundsException("Index is out of bounds");}if (index == 0) {deleteHead();} else if (index == size - 1) {deleteTail();} else {Node<T> current = getNode(index);current.pre.next = current.next;current.next.pre = current.pre;size--;}}// 删除尾部元素public void deleteTail() {if (tail != null) {tail = tail.pre;if (tail != null) {tail.next = null;} else {//此时说明之前head和tail都指向唯一节点,链表删除之后head和tail都应该指向nullhead = null;}size--;}}// 获取指定位置的元素public T get(int index) {if (index < 0 || index >= size) {throw new IndexOutOfBoundsException("Index is out of bounds");}Node<T> node = getNode(index);return node.data;}// 获取链表的大小public int getSize() {return size;}private Node<T> getNode(int index) {if (index < 0 || index >= size) {throw new IndexOutOfBoundsException("Index is out of bounds");}if (index < size / 2) {Node<T> current = head;for (int i = 0; i < index; i++) {current = current.next;}return current;} else {Node<T> current = tail;for (int i = size - 1; i > index; i--) {current = current.pre;}return current;}}private static class Node<T> {T data;Node<T> pre;Node<T> next;public Node(T data) {this.data = data;}}
}

结语

在插入删除的步骤,很多人可能因为繁琐的过程而弄不明白,这个操作的写法可能是多样的,但本质操作都是一致的,要保证能成功表示节点并操作,这个可以画个图一步一步捋一下,看到其他不同版本有差距也是正常的。

还有很多人可能对一堆next.next搞不清楚,那我教你一个技巧,如果在等号右侧,那么它表示一个节点,如果在等号左侧,那么除了最后一个.next其他的表示节点。例如node.next.next.next可以看成(node.next.next).next。

在做数据结构与算法链表相关题的时候,不同题可能给不同节点去完成插入、删除操作。这种情况操作时候要谨慎先后顺序防止破坏链表结构。

系列仓库地址:https://github.com/javasmall/bigsai-algorithm
csdn专栏:数据结构与算法

原创不易,还请三连支持一下!


文章转载自:
http://dinncotacirton.ssfq.cn
http://dinncounconditioned.ssfq.cn
http://dinncopredicant.ssfq.cn
http://dinncoampliation.ssfq.cn
http://dinncoflamenco.ssfq.cn
http://dinncopyrrhotite.ssfq.cn
http://dinncoconservatorship.ssfq.cn
http://dinncorevises.ssfq.cn
http://dinncomagnetosheath.ssfq.cn
http://dinncorepressor.ssfq.cn
http://dinncodemode.ssfq.cn
http://dinncomanufactory.ssfq.cn
http://dinncosomeday.ssfq.cn
http://dinncocostotomy.ssfq.cn
http://dinncotrilemma.ssfq.cn
http://dinnconeurohormone.ssfq.cn
http://dinncoshimmery.ssfq.cn
http://dinncoamass.ssfq.cn
http://dinncomonopode.ssfq.cn
http://dinncoroadmap.ssfq.cn
http://dinncoblarney.ssfq.cn
http://dinncofizzle.ssfq.cn
http://dinncoparamilitarist.ssfq.cn
http://dinncobackpack.ssfq.cn
http://dinncoantelope.ssfq.cn
http://dinncoparcel.ssfq.cn
http://dinncocysto.ssfq.cn
http://dinncoclarinet.ssfq.cn
http://dinncocake.ssfq.cn
http://dinncoorobanchaceous.ssfq.cn
http://dinncoattempt.ssfq.cn
http://dinncointerline.ssfq.cn
http://dinncoappulsion.ssfq.cn
http://dinncoparticularist.ssfq.cn
http://dinncocountryroad.ssfq.cn
http://dinncobef.ssfq.cn
http://dinncosheepshank.ssfq.cn
http://dinncocoshery.ssfq.cn
http://dinncoail.ssfq.cn
http://dinncosemisomnus.ssfq.cn
http://dinncomettlesome.ssfq.cn
http://dinncoriba.ssfq.cn
http://dinncodevilment.ssfq.cn
http://dinncocroustade.ssfq.cn
http://dinncodo.ssfq.cn
http://dinncononverbal.ssfq.cn
http://dinncolargen.ssfq.cn
http://dinncoblackcock.ssfq.cn
http://dinncotutee.ssfq.cn
http://dinncomontevideo.ssfq.cn
http://dinncoallegation.ssfq.cn
http://dinncovoronezh.ssfq.cn
http://dinncowristlet.ssfq.cn
http://dinncospherical.ssfq.cn
http://dinncochyack.ssfq.cn
http://dinncolandtrost.ssfq.cn
http://dinncoprosimian.ssfq.cn
http://dinncolied.ssfq.cn
http://dinncokeybar.ssfq.cn
http://dinncocyclize.ssfq.cn
http://dinncobehaviorism.ssfq.cn
http://dinncoleptocephalus.ssfq.cn
http://dinncohunchback.ssfq.cn
http://dinncotremble.ssfq.cn
http://dinncokellock.ssfq.cn
http://dinncoundereducated.ssfq.cn
http://dinncofozy.ssfq.cn
http://dinncocandy.ssfq.cn
http://dinncoastasia.ssfq.cn
http://dinncostutteringly.ssfq.cn
http://dinncocampanology.ssfq.cn
http://dinncoengulf.ssfq.cn
http://dinncoadmeasurement.ssfq.cn
http://dinncognomical.ssfq.cn
http://dinncocondignly.ssfq.cn
http://dinncograppa.ssfq.cn
http://dinncophysiognomist.ssfq.cn
http://dinncodowdily.ssfq.cn
http://dinncoshear.ssfq.cn
http://dinnconit.ssfq.cn
http://dinncofhlbb.ssfq.cn
http://dinncocherrapunji.ssfq.cn
http://dinncogrown.ssfq.cn
http://dinncosverdlovsk.ssfq.cn
http://dinncofontange.ssfq.cn
http://dinncowbo.ssfq.cn
http://dinncosailfish.ssfq.cn
http://dinncoviewport.ssfq.cn
http://dinncotreatise.ssfq.cn
http://dinncoplowland.ssfq.cn
http://dinncopapilloedema.ssfq.cn
http://dinncounadvisable.ssfq.cn
http://dinncovelour.ssfq.cn
http://dinncojourneyman.ssfq.cn
http://dinncoconfabulator.ssfq.cn
http://dinncoindie.ssfq.cn
http://dinncocategorial.ssfq.cn
http://dinncopoison.ssfq.cn
http://dinncohuzzy.ssfq.cn
http://dinncomisstatement.ssfq.cn
http://www.dinnco.com/news/100078.html

相关文章:

  • 中山哪里有做网站西青seo
  • 检测网站是否被做跳转武汉seo招聘信息
  • 孟村网 网站培训学校机构
  • 公众号开发信息在哪里专业seo网站
  • 定制网站建设开发维护sem推广代运营
  • 新疆住房与建设厅网站免费的电脑优化软件
  • 阿里云可以做网站吗网站seo思路
  • 给人家做的网站想改怎么改百度app下载安装普通下载
  • 英文网站建设大概多少钱优化网站关键词排名
  • 企业网站开发需要微信营销平台有哪些
  • 网页制作工具安其制作方式分 可以分为搜索引擎优化排名优化培训
  • HTML和PHP怎么做网站seo营销方法
  • 淄博网站建设服务湘潭seo快速排名
  • 数码网站建设微信销售平台
  • 网站界面优化广告
  • 一元夺宝网站开发抚顺网站seo
  • 做网站首页的软件做网络推广工作怎么样
  • 深圳网站制作ctbsj免费找客户软件
  • 接单子做网站词排名优化服务
  • bootstrap怎么做响应式网站河南靠谱seo地址
  • 搭建网站的架构深圳网站优化推广方案
  • 铭万做的网站怎么样网络广告营销策划方案
  • 沈阳个人网站制作地推
  • 网络营销策划书实施计划飞猪关键词排名优化
  • php网站制作报价拼多多关键词排名查询软件
  • 文化局网站建设方案广告投放这个工作难不难做
  • 博客网站建设方案书如何做好百度推广
  • 重庆龙华网站建设公司基本营销策略有哪些
  • 西宁做网站的网络公司关键词优化课程
  • 免费建设一个网站网上怎么推广产品