数据结构 链表
AI-摘要
切换
Tianli GPT
AI初始化中...
介绍自己 🙈
生成本文简介 👋
推荐相关文章 📖
前往主页 🏠
前往爱发电购买
数据结构 链表
TONG HUI单向链表
定义链表节点
1
2
3
4
5
6
7
8class Node<T>{
T data;
Node next;
public Node(T data){
this.data = data;
this.next = null;
}
}定义链表
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
46class LinkedList<T>{
private Node first;
private Node last;
private int size = 0;
public LinkedList(){
this.first =null;
this.last =null;
}
public boolean isEmpty(){return first == null;}
public void toString() {
Node<T> newNode = first;
while(newNode != null){
System.out.print("["+newNode.data+"]");
newNode = newNode.next;
}
}
public T get(int i){
Node<T> newNode = first;
if(i >= this.size) return null;
if(i == 0){
return newNode.data;
}
return (T) getPrev(i).next.data;
}
public void set(int i, T data){}
public int size() { return size;}
public void add(T data){ }
public void remove(int i){ }
public void remove(Object data){}
private Node<T> getPrev(int i){
Node<T> newNode = first;
Node<T> itemNode = null;
int index = 0;
while(newNode != null){
if(index == i){
return itemNode;
}
itemNode = newNode;
newNode = newNode.next;
index++;
}
return null;
}
}添加链表节点
1
2
3
4
5
6
7
8
9
10
11public void add(T data){
Node<T> newNode = new Node(data);
if(isEmpty()){
this.first = newNode;
this.last = newNode;
}else{
this.last.next = newNode;
this.last = newNode;
}
this.size++;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public void set(int i, T data){
if(i > this.size){
System.out.println("LinkedList is size:"+this.size+", "+ i);
return;
}
if (i == this.size){
this.add(data);
}else if (i == 0){
Node<T> newNode = new Node(data);
newNode.next = this.first;
this.first = newNode;
this.size++;
}else{
Node<T> itemNode = this.getPrev(i);
Node<T> newNode = new Node(data);
newNode.next = itemNode.next;
itemNode.next = newNode;
this.size++;
}
}删除链表节点
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
34public void remove(int i){
Node<T> newNode = first;
if(i >= this.size) return;
if(i == 0){
this.first = newNode.next;
return;
}
Node<T> itemNode = this.getPrev(i);
if (itemNode.next.next != null){
itemNode.next = itemNode.next.next;
}else {
itemNode.next = null;
}
}
public void remove(Object data){
Node<T> newNode = first;
Node<T> itemNode = null;
while(newNode != null){
if(newNode.data.equals(data)){
if(itemNode == null){
this.first = newNode.next;
} else{
itemNode.next = newNode.next;
}
break;
}
itemNode = newNode;
newNode = newNode.next;
}
}
双向链表
定义链表节点
1
2
3
4
5
6
7
8
9
10class Node<T>{
Node prev;
T data;
Node next;
public Node(T data){
this.data = data;
this.next = null;
this.prev = null;
}
}定义链表
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
41class LinkedList<T>{
private Node first;
private Node last;
private int size = 0;
public LinkedList(){
this.first =null;
this.last =null;
}
public boolean isEmpty(){return first == null;}
public void toString() {
Node<T> newNode = first;
while(newNode != null){
System.out.print("["+newNode.data+"]");
newNode = newNode.next;
}
}
public T get(int i){
Node<T> newNode = first;
if(i >= this.size) return null;
if(i == 0){
return newNode;
}
int index = 0;
while(newNode != null){
if(index == i){
return newNode;
}
newNode = newNode.next;
index++;
}
return null;
}
public void set(int i, T data){}
public int size() { return size;}
public void add(T data){ }
public void remove(int i){ }
public void remove(Object data){}
}添加链表节点
1
2
3
4
5
6
7
8
9
10
11
12public void add(T data){
Node<T> newNode = new Node(data);
if(isEmpty()){
this.first = newNode;
this.last = newNode;
}else{
newNode.prev = this.last;
this.last.next = newNode;
this.last = newNode;
}
this.size++;
}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
27public void set(int i, T data){
if(i > this.size){
System.out.println("LinkedList is size:"+this.size+", "+ i);
return;
}
if (i == this.size){
this.add(data);
}else if (i == 0){
Node<T> newNode = new Node(data);
newNode.next = this.first;
this.first.prev = newNode;
this.first = newNode;
this.size++;
}else{
Node<T> itemNode = this.get(i);
Node<T> newNode = new Node(data);
itemNode.prev.next = newNode;
newNode.prev = itemNode.prev;
newNode.next = itemNode;
itemNode.prev = newNode;
this.size++;
}
}删除链表节点
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
35public void remove(int i){
Node<T> newNode = first;
if(i >= this.size){
System.out.println("LinkedList is size:"+this.size+", "+ i);
return;
}
if(i == 0){
this.first = newNode.next;
this.first.prev = null;
this.size--;
return;
}
Node<T> itemNode = this.get(i);
itemNode.prev.next = itemNode.next;
itemNode.next.prev = itemNode.prev;
this.size--;
}
public void remove(Object data){
Node<T> newNode = first;
while(newNode != null){
if(newNode.data.equals(data)){
newNode.prev.next = newNode.next;
newNode.next.prev = newNode.prev;
this.size--;
break;
}
newNode = newNode.next;
}
}
环形链表
定义链表节点
同:单向链表
定义链表
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
49class LinkedList<T>{
private Node first;
private Node last;
private int size = 0;
public LinkedList(){
this.first =null;
this.last =null;
}
public boolean isEmpty(){return first == null;}
public void toString() {
if (this.size == 0){
return;
}
Node<T> newNode = first;
do {
System.out.print("["+newNode.data+"]");
newNode = newNode.next;
}while (newNode != first);
}
public T get(int i){
Node<T> newNode = first;
if(i >= this.size) return null;
if(i == 0){
return newNode.data;
}
return (T) getPrev(i).next.data;
}
public void set(int i, T data){}
public int size() { return size;}
public void add(T data){ }
public void remove(int i){ }
public void remove(Object data){}
private Node<T> getPrev(int i){
Node<T> newNode = first;
Node<T> itemNode = null;
int index = 0;
while(newNode != null){
if(index == i){
return itemNode;
}
itemNode = newNode;
newNode = newNode.next;
index++;
}
return null;
}
}添加链表节点
1
2
3
4
5
6
7
8
9
10
11public void add(T data){
Node<T> newNode = new Node(data);
if(isEmpty()){
this.first = newNode;
}else{
this.last.next = newNode;
}
this.last = newNode;
this.last.next = this.first;
this.size++;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23public void set(int i, T data){
if(i > this.size){
System.out.println("LinkedList is size:"+this.size+", "+ i);
return;
}
if (i == this.size){
this.add(data);
}else if (i == 0){
Node<T> newNode = new Node(data);
newNode.next = this.first;
this.first = newNode;
this.last.next = this.first;
this.size++;
}else{
Node<T> itemNode = this.getPrev(i);
Node<T> newNode = new Node(data);
newNode.next = itemNode.next;
itemNode.next = newNode;
this.size++;
}
}删除链表节点
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
36public void remove(int i){
Node<T> newNode = first;
if(i >= this.size) return;
if(i == 0){
this.first = newNode.next;
this.last.next = this.first;
return;
}
Node<T> itemNode = this.getPrev(i);
if (itemNode.next.next != null){
itemNode.next = itemNode.next.next;
}else {
itemNode.next = null;
}
}
public void remove(Object data){
Node<T> newNode = first;
Node<T> itemNode = null;
while(newNode != null){
if(newNode.data.equals(data)){
if(itemNode == null){
this.first = newNode.next;
this.last.next = this.first;
} else{
itemNode.next = newNode.next;
}
break;
}
itemNode = newNode;
newNode = newNode.next;
}
}
评论
匿名评论隐私政策
✅ 你无需删除空行,直接评论以获取最佳展示效果