File tree Expand file tree Collapse file tree 3 files changed +134
-0
lines changed
src/com/fantasy/datastructure/linkedlist Expand file tree Collapse file tree 3 files changed +134
-0
lines changed Original file line number Diff line number Diff line change 3535## 链表
3636
3737* [ 实现一个单链表,支持增删操作] [ single-linked-list ]
38+ * [ 实现一个循环链表,支持增删操作] [ circular-linked-list ]
3839
3940[ single-linked-list ] : ./src/com/fantasy/datastructure/linkedlist/SingleLinkedList.java
41+ [ circular-linked-list ] : ./src/com/fantasy/datastructure/linkedlist/CircularLinkedList.java
4042
4143## 算法
4244
Original file line number Diff line number Diff line change 1+ package com .fantasy .datastructure .linkedlist ;
2+
3+ /**
4+ * 循环链表
5+ *
6+ * <pre>
7+ * author : Fantasy
8+ * version : 1.0, 2020-07-03
9+ * since : 1.0, 2020-07-03
10+ * </pre>
11+ */
12+ public class CircularLinkedList {
13+ private ListNode head ;
14+ private ListNode tail ;
15+
16+ public CircularLinkedList () {
17+ head = null ;
18+ tail = null ;
19+ }
20+
21+ public void insertHead (int value ) {
22+ insertHead (new ListNode (value ));
23+ }
24+
25+ public void insertHead (ListNode node ) {
26+ if (head == null ) {
27+ node .next = node ;
28+ head = tail = node ;
29+ } else {
30+ node .next = head ;
31+ tail .next = node ;
32+ head = node ;
33+ }
34+ }
35+
36+ public void insertTail (int value ) {
37+ insertTail (new ListNode (value ));
38+ }
39+
40+ public void insertTail (ListNode node ) {
41+ if (head == null ) {
42+ node .next = node ;
43+ head = tail = node ;
44+ } else {
45+ node .next = head ;
46+ tail .next = node ;
47+ tail = node ;
48+ }
49+ }
50+
51+ public void deleteHead () {
52+ if (head == null ) {
53+ return ;
54+ }
55+
56+ if (head == tail ) {
57+ head = tail = null ;
58+ return ;
59+ }
60+
61+ head = head .next ;
62+ tail .next = head ;
63+ }
64+
65+ public void deleteTail () {
66+ if (tail == null ) {
67+ return ;
68+ }
69+
70+ if (head == tail ) {
71+ head = tail = null ;
72+ return ;
73+ }
74+
75+ ListNode temp = head ;
76+ while (temp .next != tail ) {
77+ temp = temp .next ;
78+ }
79+
80+ temp .next = head ;
81+ tail = temp ;
82+ }
83+
84+ public String toString () {
85+ StringBuilder sb = new StringBuilder ();
86+ sb .append ("[" );
87+ for (ListNode node = head ; node != null ; node = node .next ) {
88+ sb .append (node .val );
89+ if (node .next != head ) {
90+ sb .append ("," ).append (" " );
91+ } else {
92+ break ;
93+ }
94+ }
95+ sb .append ("]" );
96+ return sb .toString ();
97+ }
98+
99+ }
Original file line number Diff line number Diff line change 1+ package com .fantasy .datastructure .linkedlist .test ;
2+
3+ import com .fantasy .datastructure .linkedlist .CircularLinkedList ;
4+
5+ /**
6+ * “循环链表”的测试类
7+ *
8+ * <pre>
9+ * author : Fantasy
10+ * version : 1.0, 2020-07-03
11+ * since : 1.0, 2020-07-03
12+ * </pre>
13+ */
14+ public class CircularLinkedListTest {
15+
16+ public static void main (String [] args ) {
17+ CircularLinkedList list = new CircularLinkedList ();
18+
19+ list .insertHead (1 );
20+ System .out .println (list ); // [1]
21+
22+ list .insertHead (2 );
23+ list .insertTail (3 );
24+ System .out .println (list ); // [2, 1, 3]
25+
26+ list .deleteHead ();
27+ System .out .println (list ); // [1, 3]
28+
29+ list .deleteTail ();
30+ System .out .println (list ); // [1]
31+ }
32+
33+ }
You can’t perform that action at this time.
0 commit comments