【Java数据结构】实现单链表

简介: 【Java数据结构】实现单链表

 

MySingleLinkedList.java

1. public class MySingleLinkedList {
2. 
3. static class ListNode {
4. public int value;
5. public ListNode next;
6. 
7. public ListNode(int value) {
8. this.value = value;
9.         }
10.     }
11. 
12. 
13. public ListNode head;
14. 
15. //头插法
16. public void addFirst(int data) {
17. ListNode node = new ListNode(data);
18.         node.next = head;
19.         head = node;
20.     }
21. 
22. //尾插法
23. public void addLast(int data) {
24. ListNode node = new ListNode(data);
25. //1.链表为空
26. if(this.head == null) {
27. this.head = node;
28.         } else {
29. //2.链表不为空
30. ListNode cur = this.head;
31. while(cur.next != null) {
32.                 cur = cur.next;
33.             }
34.             cur.next = node;
35.         }
36.     }
37. 
38. public void addIndex(int index,int data) throws MySingleListIndexOutOfException{
39. //1.先检查插入位置是否合法
40.         checkAddIndex(index);
41. //2.分两种情况:1.头插 2.中间位置和尾插
42. ListNode node = new ListNode(data);
43. if(this.head == null) {
44. this.head = node;
45. return;
46.         }
47. if(index == 0) {
48.             addFirst(data);
49. return;
50.         }
51. ListNode cur = findAddIndexSubOne(index);
52.         node.next = cur.next;
53.         cur.next = node;
54.     }
55. private void checkAddIndex(int index) {
56. if(index < 0 || index > this.size()) {
57. throw new MySingleListIndexOutOfException("任意位置插入时,index不合法!");
58.         }
59.     }
60. //找到待插入位置的前一个结点
61. private ListNode findAddIndexSubOne(int index) {
62. ListNode cur = this.head;
63. while(index - 1 != 0) {
64.             cur = cur.next;
65.             index--;
66.         }
67. return cur;
68.     }
69. 
70. public boolean contains(int key) {
71. if(this.head == null) {
72.             System.out.println("链表为空!");
73. return false;
74.         }
75. ListNode cur = this.head;
76. while(cur != null) {
77. if(cur.value == key) {
78. return true;
79.             }
80.             cur = cur.next;
81.         }
82. return false;
83.     }
84. 
85. //删除第一次出现关键字为key的节点
86. public void remove(int key) {
87. //1.判断有无结点
88. if(this.head == null) {
89.             System.out.println("链表为空,不能删除!");
90. return;
91.         }
92. 
93. //2.删第一个
94. if(this.head.value == key) {
95. this.head = this.head.next;
96. return;
97.         }
98. //3.删后面的
99. ListNode cur = this.head;
100.         cur = removeSubOne(key,cur);
101. if(cur == null) {
102.             System.out.println("链表中没有这个元素!");
103. return;
104.         }
105.         cur.next = cur.next.next;
106.     }
107. private ListNode removeSubOne(int key, ListNode cur) {
108. while(cur.next != null) {
109. if(cur.next.value == key) {
110. return cur;
111.             }
112.             cur = cur.next;
113.         }
114. return null;
115.     }
116. 
117. public void removeAllKey(int key) {
118. //1.判断有无结点
119. if (this.head == null) {
120.             System.out.println("链表为空,不能删除!");
121. return;
122.         }
123. 
124. //处理中间和尾巴
125. ListNode cur = this.head;
126. while (cur != null) {
127. //removeSubOne函数在上一个删除方法里头
128.             cur = removeSubOne(key, cur);
129. if (cur != null) {
130.                 cur.next = cur.next.next;
131.             }
132.         }
133. 
134. //处理头
135. if (this.head.value == key) {
136. this.head = this.head.next;
137.         }
138.     }
139. 
140. //得到单链表的长度
141. public int size() {
142. ListNode cur = this.head;
143. int count = 0;
144. while(cur != null) {
145.             count++;
146.             cur = cur.next;
147.         }
148. return count;
149.     }
150. public void display() {
151. ListNode cur = this.head;
152. while(cur != null) {
153.             System.out.print(cur.value+" ");
154.             cur = cur.next;
155.         }
156.         System.out.println();
157.     }
158. public void clear() {
159. this.head = null;
160.     }
161. 
162. }

MySingleListIndexOutOfException.java

1. public class MySingleListIndexOutOfException extends RuntimeException{
2. public MySingleListIndexOutOfException() {
3.     }
4. 
5. public MySingleListIndexOutOfException(String message) {
6. super(message);
7.     }
8. }

Test.java

1. public class Test {
2. public static void main(String[] args) {
3.         MySingleLinkedList mySingleLinkedList=new MySingleLinkedList();
4.         mySingleLinkedList.addFirst(1);
5.         mySingleLinkedList.addFirst(2);
6.         mySingleLinkedList.addFirst(3);
7.         mySingleLinkedList.display();
8.         System.out.println("=============================");
9.         mySingleLinkedList.addLast(9);
10.         mySingleLinkedList.addLast(10);
11.         mySingleLinkedList.addLast(10);
12.         mySingleLinkedList.display();
13.         System.out.println("================================");
14.         System.out.println(mySingleLinkedList.size());
15.         System.out.println("==================================");
16.         mySingleLinkedList.addIndex(0,999);
17.         mySingleLinkedList.addIndex(2,888);
18.         mySingleLinkedList.addIndex(7,10001);
19.         mySingleLinkedList.display();
20.         System.out.println("=================================");
21.         mySingleLinkedList.remove(2);
22.         mySingleLinkedList.display();;
23.         mySingleLinkedList.removeAllKey(10);
24.         mySingleLinkedList.display();
25.         System.out.println("===================================");
26.         System.out.println(mySingleLinkedList.contains(888));
27. 
28.     }
29. }

测试结果:


相关文章
|
2月前
|
存储 编译器 C语言
【数据结构】C语言实现单链表万字详解(附完整运行代码)
【数据结构】C语言实现单链表万字详解(附完整运行代码)
46 0
|
2月前
|
存储
【数据结构入门指南】单链表
【数据结构入门指南】单链表
43 0
|
2月前
|
存储 算法 索引
数据结构与算法:单链表
朋友们大家好,本节来到数据结构与算法的新内容:单链表 在上篇文章中,我们知道顺序表通常需要预分配一个固定大小的内存空间, 通常以二倍的大小进行增容,可能会造成空间的浪费,本篇文章我们介绍的链表可以解决这个问题
|
3天前
|
C++
数据结构(循环单链表
数据结构(循环单链表
9 2
|
3天前
|
C++
数据结构(单链表
数据结构(单链表
6 0
|
6天前
|
存储
[数据结构]单链表(从0->1)
[数据结构]单链表(从0->1)
|
16天前
|
存储 安全 Java
Java程序员必须掌握的数据结构:HashMap
HashMap底层原理实现是每个Java Boy必须掌握的基本技能,HashMap也是业务开发每天都需要遇到的好伙伴。如此基础且核心的底层数据结构,JDK也给其赋予了线程安全的功能,我们来看看~
30 2
Java程序员必须掌握的数据结构:HashMap
|
17天前
|
存储 安全 Java
Java并发编程中的高效数据结构:ConcurrentHashMap解析
【4月更文挑战第25天】在多线程环境下,高效的数据访问和管理是至关重要的。Java提供了多种并发集合来处理这种情境,其中ConcurrentHashMap是最广泛使用的一个。本文将深入分析ConcurrentHashMap的内部工作原理、性能特点以及它如何在保证线程安全的同时提供高并发性,最后将展示其在实际开发中的应用示例。
|
17天前
|
存储
数据结构基础:一篇文章教你单链表(头插,尾插,查找,头删等的解析和代码)
数据结构基础:一篇文章教你单链表(头插,尾插,查找,头删等的解析和代码)
|
20天前
|
存储
数据结构:4、链表之单链表
数据结构:4、链表之单链表
12 0