-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedList.java
More file actions
101 lines (94 loc) · 1.8 KB
/
CircularLinkedList.java
File metadata and controls
101 lines (94 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.lzz.linkedlist;
import com.lzz.vo.Node1;
public class CircularLinkedList {
public int size;
public Node1 head;
public CircularLinkedList() {
size = 0;
head = null;
}
public void addNode(int data) {
/*
* 在链表尾部添加一个节点
*/
Node1 node = new Node1(data);
if (head == null) {
head = node;
node.next = head;
size++;
} else {
if (head.next.equals(head)) {
head.next = node;
node.next = head;
size++;
} else {
Node1 temp = head;
while (temp.next != head) {
temp = temp.next;
}
temp.next = node;
node.next = head;
size++;
}
}
}
public void delNode(int data) {
/*
* 根据Data删除节点
*/
Node1 temp = head;
while (true) {
if (temp.next.getData() == data) {
if (temp.next == head) {
head = temp.next.next;
size--;
break;
} else {
temp.next = temp.next.next;
size--;
break;
}
} else {
temp = temp.next;
}
}
}
public Node1 findByIndex(int index) {
/*
* 查询index后一个节点的信息
*/
Node1 curNode = head;
int i = 0;
while (i != index) {
curNode = curNode.next;
i++;
}
return curNode;
}
public void Display() {
/*
* 展示链表结构
*/
if (size > 0) {
Node1 node = head;
int tempSize = size;
if (tempSize == 1) {
System.out.println("[" + node.getData() + "]");
return;
}
while (tempSize > 0) {
if (node.equals(head)) {
System.out.print("[" + node.getData() + "->");
} else if (node.next.equals(head)) {
System.out.println(node.getData() + "]");
} else {
System.out.print(node.getData() + "->");
}
node = node.next;
tempSize--;
}
} else {
System.out.println("[]");
}
}
}