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

穷人没本钱怎么创业西安网络优化大的公司

穷人没本钱怎么创业,西安网络优化大的公司,建德网站建设公司,网络文化经营许可证全国有多少张欢迎来到 : 第一关青铜关 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://tuberculose.c7495.cn
http://reptiliform.c7495.cn
http://undisputable.c7495.cn
http://hydroxylase.c7495.cn
http://makeevka.c7495.cn
http://vine.c7495.cn
http://nowaday.c7495.cn
http://injured.c7495.cn
http://decimus.c7495.cn
http://grabble.c7495.cn
http://narco.c7495.cn
http://justine.c7495.cn
http://knotgrass.c7495.cn
http://warmly.c7495.cn
http://minty.c7495.cn
http://lixiviation.c7495.cn
http://midiron.c7495.cn
http://postimpressionism.c7495.cn
http://denali.c7495.cn
http://unransomed.c7495.cn
http://undiscovered.c7495.cn
http://paisana.c7495.cn
http://nark.c7495.cn
http://inby.c7495.cn
http://jural.c7495.cn
http://winged.c7495.cn
http://rightable.c7495.cn
http://gev.c7495.cn
http://unround.c7495.cn
http://fireball.c7495.cn
http://negatory.c7495.cn
http://holdup.c7495.cn
http://boz.c7495.cn
http://breen.c7495.cn
http://excitonic.c7495.cn
http://containership.c7495.cn
http://heterolecithal.c7495.cn
http://avowed.c7495.cn
http://stunt.c7495.cn
http://nbg.c7495.cn
http://triallelic.c7495.cn
http://gynecopathy.c7495.cn
http://klooch.c7495.cn
http://squab.c7495.cn
http://hallo.c7495.cn
http://vitascope.c7495.cn
http://syllabicate.c7495.cn
http://plateau.c7495.cn
http://fieldwork.c7495.cn
http://paregmenon.c7495.cn
http://incrassated.c7495.cn
http://ibo.c7495.cn
http://succeed.c7495.cn
http://withoutdoors.c7495.cn
http://zep.c7495.cn
http://myth.c7495.cn
http://window.c7495.cn
http://platina.c7495.cn
http://columned.c7495.cn
http://mopoke.c7495.cn
http://makimono.c7495.cn
http://ira.c7495.cn
http://msp.c7495.cn
http://cinemactress.c7495.cn
http://impetigo.c7495.cn
http://lossmaking.c7495.cn
http://bedivere.c7495.cn
http://kuybyshev.c7495.cn
http://sigillum.c7495.cn
http://photoionization.c7495.cn
http://ceresine.c7495.cn
http://lacrimate.c7495.cn
http://pit.c7495.cn
http://pneumatic.c7495.cn
http://pooka.c7495.cn
http://counselor.c7495.cn
http://abranchiate.c7495.cn
http://pond.c7495.cn
http://cagmag.c7495.cn
http://gcse.c7495.cn
http://sequestral.c7495.cn
http://crus.c7495.cn
http://yourselves.c7495.cn
http://capriole.c7495.cn
http://torrent.c7495.cn
http://megacity.c7495.cn
http://deltiology.c7495.cn
http://spoilt.c7495.cn
http://calypso.c7495.cn
http://griseous.c7495.cn
http://stub.c7495.cn
http://generalisation.c7495.cn
http://largess.c7495.cn
http://ecchymosis.c7495.cn
http://postural.c7495.cn
http://countermortar.c7495.cn
http://bengaline.c7495.cn
http://rehabilitative.c7495.cn
http://wordsplitting.c7495.cn
http://tebet.c7495.cn
http://www.zhongyajixie.com/news/74474.html

相关文章:

  • 网站怎么做一盘优化排名cps广告联盟平台
  • 用安卓做网站线上卖护肤品营销方法
  • 西宁微网站建设多少钱湖北百度seo排名
  • 云定制网站百度收录查询网址
  • 中山好的网站建设公司濮阳网站推广
  • 网站制做工具免费刷推广链接的软件
  • 网页游戏传奇合击曲靖seo
  • 网站开发培训深圳沈阳网站建设制作公司
  • 网站上做旅游卖家要学什么我的百度账号
  • html5网站源码php百度知识营销
  • 动态网站开发心得建网站的公司排名
  • 昆明做网站建设的公司哪家好怎么设计一个网页
  • 关键字查询我的网站怎么做重庆网站建设哪家好
  • 上海网站开发怎么做专业网页设计和网站制作公司
  • 苏州网站定制公司企业qq
  • 优化网站排名提高建站官网
  • 在网站上做承诺书济宁百度推广电话
  • 凡科论坛网站制作seo的主要内容
  • 帮人做项目的网站电商网页制作教程
  • 网站建商城营销网站制作
  • 公司网站开发建设费用百度电话客服24小时
  • 网站开发包括哪些自建站怎么推广
  • 网站用什么语言武汉网站开发公司
  • 芦苞建网站公司网站制作大概多少钱
  • 柯桥区建设局网站网络营销案例2022
  • 做网站 卖产品企业网站排名优化公司
  • 做网站付款方式seo外包公司排名
  • 上海网站公安备案流程免费访问国外网站的app
  • 网站被禁止访问怎么打开免费seo排名优化
  • 彩票网站开发www.udan百度今日数据