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

w3school网站建设教程宁波网络优化seo

w3school网站建设教程,宁波网络优化seo,微信公众号怎么创建一个公众号,昆明市做网站欢迎来到 : 第一关青铜关 java如何创建链表链表怎么增删改查 我们先了解链表 单链表的概念 我们从简单的创建和增删改查开始. 链表的概念 线性表分为顺序表(数组组成)和链表(节点组成) . 链表又分: 单向 双向有哨兵节点 无哨兵节点循环 不循环 链表是一种物理存储单…

欢迎来到 : 第一关青铜关

  1. java如何创建链表
  2. 链表怎么增删改查

我们先了解链表

单链表的概念

我们从简单的创建和增删改查开始.

链表的概念

线性表分为顺序表(数组组成)和链表(节点组成) . 

链表又分: 

  • 单向 双向
  • 有哨兵节点 无哨兵节点
  • 循环 不循环

链表是一种物理存储单元上非连续、非顺序的存储结构,单链表就像铁链一样,元素之间互相连接。链表由一系列的结点(链表中的每一个元素称为结点也叫节点)组成, 结点可以在运行时动态生成。

 在链表中每个节点都有数据域和指针域两部分:

  数据域用来存值 , 指针域用来存放地址(下一节点的地址) . 

 举个简单的例子{1,2,3}用链表存储:

思考一下

思考一下面两个图 , 是否满足单链表的要求 , 为什么 ?

图一:

图二:

解析:

第一图是满足单链表的要求 , 因为我们说链表要求环环相扣,核心是一个结点只能有一个后继,但不代表个结点只能有一个被指向。第一个图中,c1被a2和b3同时指向,这是没关系的。这就好比法律倡导一夫一妻,你只能爱一个人,但是可以都多个人爱你。
第二图就不满足要求了,因为c1有两个后继a5和b4.另外在做题的时候要注意比较的是值还是结点,有时可能两个结点的值相等,但并不是同一个结点,例如下图中有两个结点的值都是1,但并不是同一个结点。

链表的相关概念

节点和头节点

每个点都由值和指向下一个结点的地址组成的独立的单元,称为一个结点,有时也称为节点,含义都在链表中,是一样的。
对于单链表,如果知道了第一个元素,就可以通过遍历访问整个链表,因此第一个结点最重要一般称为头结点

虚拟节点(哨兵节点)

在做题以及在工程里经常会看到虚拟结点的概念,其实就是一个结点dummyNode,其next指针指向head,也就是dummyNode.next=head.
因此,如果我们在算法里使用了虚拟结点,则要注意如果要获得head结点,或者从方法(函数)里返回的时候,则应使用dummyNode.next.
另外注意,dummyNode的val不会被使用,初始化为0或者-1等都是可以的。既然值不会使用,那虚拟结点有啥用呢?简单来说,就是为了方便我们处理首部结点,否则我们需要在代码里单独处理首部结点的问题。在链表反转里,我们会看到该方式可以大大降低解题难度

创建链表

那我们如何使用链表呢?按照面向对象的思想,我们可以设计一个类,来描述结点这个事物,用一个属性描述这个结点存储的元素,用来另外一个属性描述这个结点的下一个结点。

类名Node
构造方法Node(T t,Node next):创建Node对象
成员变量T value:存储数据 Node next:指向下一个结点

 举例 : 存储值为int类型

/*** 节点类*/
public class Node {//值int value;//地址Node next;public Node(int value, Node next) {this.value = value;this.next = next;}
}

生成链表:

    public static void main(String[] args) throws Exception {//构建结点Node<Integer> first = new Node<Integer>(11, null);Node<Integer> second = new Node<Integer>(13, null);Node<Integer> third = new Node<Integer>(12, null);Node<Integer> fourth = new Node<Integer>(8, null);Node<Integer> fifth = new Node<Integer>(9, null);//生成链表first.next = second;second.next = third;third.next = fourth;fourth.next = fifth;}

添加数据 :

    public static void main(String[] args) throws Exception {//构建结点Node<Integer> first = new Node<Integer>(11, null);Node<Integer> second = new Node<Integer>(13, null);Node<Integer> third = new Node<Integer>(12, null);Node<Integer> fourth = new Node<Integer>(8, null);Node<Integer> fifth = new Node<Integer>(9, null);//生成链表first.next = second;second.next = third;third.next = fourth;fourth.next = fifth;//添加数据Node<Integer> six= new Node<Integer>(22, null);six.next = second;first.next = six;}

删除数据: 

    public static void main(String[] args) throws Exception {//构建结点Node<Integer> first = new Node<Integer>(11, null);Node<Integer> second = new Node<Integer>(13, null);Node<Integer> third = new Node<Integer>(12, null);Node<Integer> fourth = new Node<Integer>(8, null);Node<Integer> fifth = new Node<Integer>(9, null);//生成链表first.next = second;second.next = third;third.next = fourth;fourth.next = fifth;//添加数据Node<Integer> six= new Node<Integer>(22, null);six.next = second;first.next = six;//删除数据first.next = second;}

修改数据:

修改值就很简单了找到节点直接修改就可以了:

 first.value = 100;

单向链表

单向链表是链表的一种,它由多个结点组成,每个结点都由一个数据域和一个指针域组成,数据域用来存储数据, 指针域用来指向其后继结点。链表的头结点的数据域不存储数据,指针域指向第一个真正存储数据的结点。

单向链表设计 :

类名LinkList
构造方法LinkList():创建LinkList对象
成员方法

1.public void clear():空置线性表

2.publicboolean isEmpty():判断线性表是否为空,是返回true,否返回false

3.public int length():获取线性表中元素的个数

4.public T get(int i):读取并返回线性表中的第i个元素的值

5.public void insert(T t):往线性表中添加一个元素;

6.public void insert(int i,T t):在线性表的第i个元素之前插入一个值为t的数据元素。

7.public T remove(int i):删除并返回线性表中第i个数据元素。

8.public int indexOf(T t):返回线性表中首次出现的指定的数据元素的位序号,若不存在,则

返回-11。

成员内部 类private class Node:结点类
成员变量

1.private Node head:记录首结点

2.private int N:记录链表的长度

/*** 单链表 (虚拟节点)* @param <T>*/
public class LinkList<T> {//记录头结点private Node head;//记录链表的长度private int N;public LinkList() {//初始化头结点head = new Node(null, null);N = 0;}//清空链表public void clear() {head.next = null;head.item = null;N = 0;}//获取链表的长度public int length() {return N;}//判断链表是否为空public boolean isEmpty() {return N == 0;}//获取指定位置i出的元素public T get(int i) {if (i < 0 || i >= N) {throw new RuntimeException("位置不合法!");}Node n = head.next;for (int index = 0; index < i; index++) {n = n.next;}return n.item;}//向链表中添加元素tpublic void insert(T t) {//找到最后一个节点Node n = head;while (n.next != null) {n = n.next;}Node newNode = new Node(t, null);n.next = newNode;//链表长度+1N++;}//向指定位置i处,添加元素tpublic void insert(int i, T t) {if (i < 0 || i >= N) {throw new RuntimeException("位置不合法!");}//寻找位置i之前的结点Node pre = head;for (int index = 0; index <= i - 1; index++) {pre = pre.next;}//位置i的结点Node curr = pre.next;Node newNode = new Node(t, curr);//让之前的结点指向新结点pre.next = newNode;//长度+1N++;}//删除指定位置i处的元素,并返回被删除的元素public T remove(int i) {if (i < 0 || i >= N) {throw new RuntimeException("位置不合法");}//寻找i之前的元素Node pre = head;for (int index = 0; index <= i - 1; index++) {pre = pre.next;}//当前i位置的结点Node curr = pre.next;//前一个结点指向下一个结点,删除当前结点pre.next = curr.next;//长度-1N--;return curr.item;}//查找元素t在链表中第一次出现的位置public int indexOf(T t) {Node n = head;for (int i = 0; n.next != null; i++) {n = n.next;if (n.item.equals(t)) {return i;}}return -1;}//结点类private class Node {//存储数据T item;//下一个结点Node next;public Node(T item, Node next) {this.item = item;this.next = next;}}
}

测试 : 

public class LinkTest {public static void main(String[] args) {LinkList<String> list = new LinkList<>();list.insert("aa");list.insert("bb");list.insert(1,"cc");list.insert("dd");for (int i = 0; i < list.length(); i++) {System.out.println(list.get(i));}list.remove(1);System.out.println(list.length());}
}

只要设计合理 , 都可以!

简化一点的版本 : 


/*** 单向链表*/
public class SinglyLinkedList {//哨兵(头指针)private Node head = null;//节点类private static class Node {int data;Node next;public Node(int data, Node next) {this.data = data;this.next = next;}}/*** 向链表头部插入** @param value*/public void addFirst(int value) {//1.链表为空的情况//head = new Node(value, null);//2.链表非空head = new Node(value, head);}/*** 遍历*/public void foreach(Consumer<Integer> consumer) {Node p = head;while (p != null) {consumer.accept(p.data);p = p.next;}}/*** 找到最后一个节点** @return*/private Node findLast() {if (head == null) {return null;}Node p = head;while (p.next != null) {p = p.next;}return p;}/*** 在链表尾部添加节点** @param value*/public void addLast(int value) {Node last = findLast();if (last == null) {addFirst(value);return;}last.next = new Node(value, null);}/*** 根据索引查找** @param index* @return*/private Node findNode(int index) {int i = 0;for (Node p = head; p != null; p = p.next, i++) {if (i == index) {return p;}}return null;}/*** 根据索引获取值** @param index* @return*/public int get(int index) {Node node = findNode(index);if (node == null) {throw new IllegalArgumentException(String.format("index is error"));}return node.data;}/*** 向索引位置插入数据** @param index* @param value*/public void insert(int index, int value) {if (index == 0) {addFirst(value);return;}Node node = findNode(index - 1);if (node == null) {throw new IllegalArgumentException(String.format("index is error"));}node.next = new Node(value, node.next);}/*** 删除头*/public void removeFirst() {if (head == null) {throw new IllegalArgumentException("Null");} else {head = head.next;}}/*** 按索引删除** @param index*/public void removeIndex(int index) {if (index == 0) {removeFirst();} else {Node node = findNode(index - 1);if (node == null) {throw new IllegalArgumentException("error");}if (node.next == null) {throw new IllegalArgumentException("error");}node.next = node.next.next;}}}

测试大家自己练习一下......

双向链表

双向链表也叫双向表,是链表的一种,它由多个结点组成,每个结点都由一个数据域和两个指针域组成,数据域用 来存储数据,其中一个指针域用来指向其后继结点,另一个指针域用来指向前驱结点。链表的头结点的数据域不存 储数据,指向前驱结点的指针域值为null,指向后继结点的指针域指向第一个真正存储数据的结点。

 简单写了一下 , 伙伴们自己完善和修改吧 : 

/*** 双链表*/public class TwoLinkList {//哨兵节点private Node head = new Node(null, -1, null);//节点private static class Node {Node pre;int value;Node next;public Node(Node pre, int value, Node next) {this.pre = pre;this.value = value;this.next = next;}}/*** 查找尾节点** @return*/private Node findLastNode() {Node node = head;while (node.next != null ) {node = node.next;}return node;}/*** 尾插入** @param value*/public void insert(int value) {Node lastNode = findLastNode();lastNode.next = new Node(lastNode,value,null);}/*** 头插入** @param value*/public void addFist(int value) {//插入Node node = head;node.next=new Node(head,value,head.next);}/*** 遍历*/public void forEach() {if (head.next == null) {System.out.println("null!");}else {Node p = head.next;while (p != null) {System.out.println(p.value);p = p.next;}}}
}

测试 : 

public class TwoLinkListTest {public static void main(String[] args) {TwoLinkList twoLinkList = new TwoLinkList();twoLinkList.addFist(1);twoLinkList.addFist(2);twoLinkList.addFist(3);twoLinkList.addFist(4);twoLinkList.insert(4);twoLinkList.forEach();}
}

这关就到这里了, 朋友们下一关见!


文章转载自:
http://dinncoembassador.ssfq.cn
http://dinncodisk.ssfq.cn
http://dinncooverstriking.ssfq.cn
http://dinncoblanketflower.ssfq.cn
http://dinnconorite.ssfq.cn
http://dinncobrutish.ssfq.cn
http://dinncocalciphobous.ssfq.cn
http://dinncotitus.ssfq.cn
http://dinncotarp.ssfq.cn
http://dinncoshanghailander.ssfq.cn
http://dinncodictate.ssfq.cn
http://dinncostillbirth.ssfq.cn
http://dinncofloozie.ssfq.cn
http://dinncodecouple.ssfq.cn
http://dinncosixscore.ssfq.cn
http://dinncoclon.ssfq.cn
http://dinncodrifter.ssfq.cn
http://dinncomacrophyllous.ssfq.cn
http://dinncoinjure.ssfq.cn
http://dinncocalamitously.ssfq.cn
http://dinncoresorbent.ssfq.cn
http://dinncooffending.ssfq.cn
http://dinncopsychogony.ssfq.cn
http://dinncousuriously.ssfq.cn
http://dinncosubjugate.ssfq.cn
http://dinncotela.ssfq.cn
http://dinncoaureus.ssfq.cn
http://dinncohydrolytic.ssfq.cn
http://dinncoigbo.ssfq.cn
http://dinncocageling.ssfq.cn
http://dinncoclostridial.ssfq.cn
http://dinncosynchronous.ssfq.cn
http://dinncopedocal.ssfq.cn
http://dinncohobby.ssfq.cn
http://dinncopulut.ssfq.cn
http://dinncohiemal.ssfq.cn
http://dinncoolea.ssfq.cn
http://dinncocastile.ssfq.cn
http://dinncoassoluta.ssfq.cn
http://dinncoenameling.ssfq.cn
http://dinncopreceptress.ssfq.cn
http://dinncocraterization.ssfq.cn
http://dinncononimportation.ssfq.cn
http://dinncoaccoucheuse.ssfq.cn
http://dinncohappi.ssfq.cn
http://dinncowairakite.ssfq.cn
http://dinncoanaphylactoid.ssfq.cn
http://dinncoglycosyl.ssfq.cn
http://dinncovehement.ssfq.cn
http://dinncotiros.ssfq.cn
http://dinncodopester.ssfq.cn
http://dinncotheme.ssfq.cn
http://dinncomonal.ssfq.cn
http://dinncobubbler.ssfq.cn
http://dinncopickerelweed.ssfq.cn
http://dinncomeletin.ssfq.cn
http://dinncoweeping.ssfq.cn
http://dinncoescalator.ssfq.cn
http://dinncosterility.ssfq.cn
http://dinncolooker.ssfq.cn
http://dinncoanisotropy.ssfq.cn
http://dinncoturaco.ssfq.cn
http://dinncoluminaria.ssfq.cn
http://dinncopolydomous.ssfq.cn
http://dinncothigh.ssfq.cn
http://dinncointragenic.ssfq.cn
http://dinncohematoblastic.ssfq.cn
http://dinncohesitate.ssfq.cn
http://dinncoreinsurance.ssfq.cn
http://dinncodysbarism.ssfq.cn
http://dinncofatherland.ssfq.cn
http://dinncosenarmontite.ssfq.cn
http://dinncogoyaesque.ssfq.cn
http://dinncocystocarp.ssfq.cn
http://dinncospunbonded.ssfq.cn
http://dinncosundried.ssfq.cn
http://dinncovycor.ssfq.cn
http://dinncoosmium.ssfq.cn
http://dinncoradiomimetic.ssfq.cn
http://dinncospermatologist.ssfq.cn
http://dinncokarroo.ssfq.cn
http://dinncotristylous.ssfq.cn
http://dinncomedieval.ssfq.cn
http://dinncoreengineer.ssfq.cn
http://dinncoarras.ssfq.cn
http://dinncosherbet.ssfq.cn
http://dinncorootedness.ssfq.cn
http://dinncotilt.ssfq.cn
http://dinncochristen.ssfq.cn
http://dinncohomeworker.ssfq.cn
http://dinncoabdominal.ssfq.cn
http://dinncoparlay.ssfq.cn
http://dinncomultifarious.ssfq.cn
http://dinnconoegenesis.ssfq.cn
http://dinncozirconolite.ssfq.cn
http://dinncofiltrable.ssfq.cn
http://dinncoadvantaged.ssfq.cn
http://dinncoverdian.ssfq.cn
http://dinncoenfilade.ssfq.cn
http://dinncostarlike.ssfq.cn
http://www.dinnco.com/news/127454.html

相关文章:

  • 企业网站模板 简洁深圳网站制作
  • 大连微信网站制作视频互联网推广选择隐迅推
  • 恩施网站开发百度应用市场下载安装
  • 狮山建网站企业管理培训课程网课免费
  • 做网站都需要什么软件百度手机快速排名点击软件
  • 网页游戏网站快手关键词优化排名详细步骤
  • 网站建设linux太原网站制作优化seo公司
  • 招聘网站开发需求百度推广代理商与总公司的区别
  • 建网站需要花哪些钱seo在线网站推广
  • c2b模式的代表企业有哪些百度快速排名优化工具
  • 电子商务网站推广怎么做seo编辑培训
  • 做ppt的网站兼职福建网站建设制作
  • 全国建造师信息查询天津seo网站排名优化公司
  • 网站改版需要注意网络营销能干什么工作
  • 推广app网站搜索引擎入口大全
  • 门户网站建设意见营销推广的特点
  • 不需要备案如何做网站网页版
  • 武汉市人民政府网官网济宁seo推广
  • 专业做涂料网站网站关键词在哪里看
  • dedecms win8风格网站模板深圳网络推广外包
  • 怎样做地方门户网站黄金网站app视频播放画质选择
  • 江门排名优化公司网站seo优化
  • 网站建设费入什么总账科目信息推广
  • 那个网站专门做二手衣服的网站产品推广
  • 如何做电影网站2023年新冠疫情最新消息
  • 做网站开发找哪家公司百度搜索热词排行榜
  • 做网站的人 优帮云国外免费网站域名服务器查询
  • 免费咨询服务合同范本免费版seo技术快速网站排名
  • 专门做头像的网站百度搜题
  • 响应式网站制作流程图网站seo优化公司