LinkedList与链表

LinkedList与链表目录 一 链表的概念结构 二 单向无头非循环链表的实现 三 双向无头非循环链表的实现 四 LinedList 的遍历 1 for 循环遍历 2 for each 遍历 3 使用迭代器正向遍历 4 使用迭代器反向遍历 五 ArrayList 和 LinedList 的区别 1 存储空间 2 随机访问 3 从头插入元素 4 应用场景

大家好,我是讯享网,很高兴认识大家。

目录

一、链表的概念结构

二、单向无头非循环链表的实现

三、双向无头非循环链表的实现 

四、LinedList的遍历

1、for循环遍历

2、for each遍历

3、使用迭代器正向遍历

4、使用迭代器反向遍历

五、ArrayList和LinedList的区别

1、存储空间

2、随机访问

3、从头插入元素


讯享网

4、应用场景


一、链表的概念结构

链表:是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。

​

如上图,每一个节点都会存储下一个节点的地址,那么就可以通过当前节点访问下一个节点,这些节点在逻辑上是连续的,但是在物理上不一定连续,因为每次申请的空间可能连续,可能不连续。 

实际中,链表的结构非常多样:

1、单向或双向:

​

  2、带头或不带头:

​​

 3、循环或非循环:

​​

这些情况组合起来就有8种链表结构:

​我们常用的是单向不带头非循环链表和双向不带头链表。

二、单向无头非循环链表的实现

public class SingleList { private int usedSize; static class ListNode { public int val; public ListNode next; public ListNode(int val) { this.val = val; } } public ListNode head;//表示存储当前链表的头节点的引用 public void disPlay() { ListNode ret = this.head; while (ret != null) { System.out.print(ret.val + " "); ret = ret.next; } } //查看链表中是否包含key public boolean contains(int key) { ListNode ret = this.head; while (ret != null) { if (ret.val == key) { return true; } ret = ret.next; } return false; } public int size() { /*Node ret = this.head; int count = 0; while (ret != null){ count++; ret = ret.next; } return count;*/ return this.usedSize; } //头插法 public void addFirst(int data) { ListNode node = new ListNode(data); node.next = this.head; this.head = node; this.usedSize++; } //尾插法 public void addLast(int data) { ListNode node = new ListNode(data); ListNode ret = this.head; if (head == null) { head = node; } else { while (ret.next != null) { ret = ret.next; } ret.next = node; } usedSize++; } private void checkIndex(int index) { if (index < 0 || index > usedSize) { throw new IndexNotLegalException("index位置不合法"); } } //任意位置插入 public void addIndex(int index, int data) { checkIndex(index); if (index == 0) { addFirst(data); } if (index == usedSize) { addLast(data); } ListNode node = new ListNode(data); ListNode ret = head; for (int i = 0; i < index - 1; i++) { ret = ret.next; } node.next = ret.next; ret.next = node; usedSize++; } //删除第一次出现关键字为key的节点 public void remove(int key) { if (head == null) { return; } if (head.val == key) { head = head.next; return; } ListNode cur = searchPrevOfKey(key); if (cur == null) { return; } ListNode del = cur.next; cur.next = del.next; } / * 找到关键字key的前驱 * * @param key * @return */ private ListNode searchPrevOfKey(int key) { if (head == null) { return null; } ListNode cur = head; while (cur.next != null) { if (cur.next.val == key) { return cur; } cur = cur.next; } return null; } //删除所有值为key的节点 public void removeAllKey(int key) { if (head == null) { return; } ListNode prev = head; ListNode cur = prev.next; while (cur != null) { if (cur.val == key) { prev.next = cur.next; cur = cur.next; usedSize--; } else { prev = cur; cur = cur.next; } } if (head.val == key) { head = head.next; usedSize--; } } //清空链表 public void clear() { //this.head = null; ListNode cur = this.head; while (cur != null) { ListNode curNext = cur.next; cur.next = null; cur = curNext; } this.head = null; } }

讯享网
小讯
上一篇 2025-04-03 22:27
下一篇 2025-04-07 11:09

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/94026.html