22
33/**
44 * 单链表的具体实现
5+ *
56 * @author : chen weijie
67 * @Date: 2019-02-21 11:35 PM
78 */
@@ -14,7 +15,6 @@ public class SingleLinkedList {
1415 private Node head ;
1516
1617
17-
1818 private class Node {
1919
2020 //每个节点的数据
@@ -31,38 +31,33 @@ public Node(Object data) {
3131
3232 /**
3333 * 单链表的表头添加元素
34+ *
3435 * @param data
3536 * @return
3637 */
3738 public Object addHead (Object data ) {
3839
3940 Node newHead = new Node (data );
40-
4141 if (size == 0 ) {
4242 head = newHead ;
4343 } else {
44- //新头节点的下个节点是旧的head节点
4544 newHead .next = head ;
46- //新加入的节点为SingleLinkedList的头节点
4745 head = newHead ;
4846 }
49-
5047 size ++;
51- return data ;
48+
49+ return head ;
5250 }
5351
5452
5553 /**
5654 * 删除链表头节点
55+ *
5756 * @return
5857 */
5958 public Object deleteHead () {
60-
6159 Object data = head .data ;
62-
63- //新头节点为旧头节点的下个节点
6460 head = head .next ;
65-
6661 size --;
6762 return data ;
6863 }
@@ -75,37 +70,55 @@ public Object deleteHead() {
7570 */
7671 public Node find (Object data ) {
7772
78- Node curr = head ;
79- //只是查找,不改变链表的个数,所以要新建tempSize变量
80- int tempSize = size ;
73+ Node current = head ;
8174
75+ int tempSize = size ;
8276 while (tempSize > 0 ) {
83- if (data .equals (curr .data )) {
84- return curr ;
77+ if (data .equals (current .data )) {
78+ return current ;
79+ } else {
80+ current = current .next ;
81+ tempSize --;
8582 }
86- curr = curr .next ;
87- tempSize --;
83+
8884 }
8985 return null ;
86+
9087 }
9188
9289 /**
9390 * 删除指定元素,删除成功返回true
91+ *
9492 * @param data
9593 * @return
9694 */
97- public boolean delete (Object data ){
98-
99-
100-
101-
102-
103-
104-
105-
95+ public boolean delete (Object data ) {
10696
97+ if (size == 0 ) {
98+ return true ;
99+ }
100+ Node current = head ;
101+ Node previous = head ;
102+
103+ while (current .data != data ) {
104+ if (current .next == null ) {
105+ return false ;
106+ } else {
107+ previous = current ;
108+ current = current .next ;
109+ }
110+ }
107111
108- return false ;
112+ //如果删除的节点是第一个节点
113+ if (current == head ) {
114+ head = current .next ;
115+ size --;
116+ } else {
117+ //删除的节点不是第一个节点
118+ previous .next = current .next ;
119+ size --;
120+ }
121+ return true ;
109122 }
110123
111124
@@ -119,6 +132,59 @@ public boolean isEmpty() {
119132 }
120133
121134
135+ //显示节点信息
136+ public void display () {
137+ if (size > 0 ) {
138+ Node node = head ;
139+ int tempSize = size ;
140+ //当前链表只有一个节点
141+ if (tempSize == 1 ) {
142+ System .out .println ("[" + node .data + "]" );
143+ return ;
144+ }
145+ while (tempSize > 0 ) {
146+ if (node .equals (head )) {
147+ System .out .print ("[" + node .data + "->" );
148+ } else if (node .next == null ) {
149+ System .out .print (node .data + "]" );
150+ } else {
151+ System .out .print (node .data + "->" );
152+ }
153+ node = node .next ;
154+ tempSize --;
155+ }
156+ System .out .println ();
157+ } else {
158+ //如果链表一个节点都没有,直接打印[]
159+ System .out .println ("[]" );
160+ }
161+
162+ }
163+
164+
165+ public static void main (String [] args ) {
166+
167+ SingleLinkedList singleLinkedList = new SingleLinkedList ();
168+ singleLinkedList .addHead ("A" );
169+ singleLinkedList .addHead ("B" );
170+ singleLinkedList .addHead ("C" );
171+ singleLinkedList .addHead ("D" );
172+
173+ singleLinkedList .display ();
174+
175+ singleLinkedList .delete ("B" );
176+
177+ singleLinkedList .display ();
178+
179+ System .out .println ("find:" + singleLinkedList .find ("D" ));
180+
181+
182+ }
183+
184+
185+
186+
187+
122188
123189
124190
0 commit comments