-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFourLinkedListNode.java
More file actions
109 lines (87 loc) · 2.54 KB
/
FourLinkedListNode.java
File metadata and controls
109 lines (87 loc) · 2.54 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
102
103
104
105
106
107
108
109
/**
* 四项链表节点类,每个节点有四个方向的指针,分别是up、down、left、right
* 作用:可以表示一个二维矩阵
*/
// 四项链表节点类
public class FourLinkedListNode<T> {
private T data;
private FourLinkedListNode<T> up;
private FourLinkedListNode<T> down;
private FourLinkedListNode<T> left;
private FourLinkedListNode<T> right;
// 构造函数
public FourLinkedListNode(T data) {
this.data = data;
}
// 将所有Getter Setter生成出来
public T setData(T data) {
this.data = data;
return this.data;
}
public T getData() {
return data;
}
public FourLinkedListNode<T> getUp() {
return up;
}
public void setUp(FourLinkedListNode<T> up) {
this.up = up;
}
public FourLinkedListNode<T> getDown() {
return down;
}
public void setDown(FourLinkedListNode<T> down) {
this.down = down;
}
public FourLinkedListNode<T> getLeft() {
return left;
}
public void setLeft(FourLinkedListNode<T> left) {
this.left = left;
}
public FourLinkedListNode<T> getRight() {
return right;
}
public void setRight(FourLinkedListNode<T> right) {
this.right = right;
}
public static <T> FourLinkedListNode<T> create(T data) {
return new FourLinkedListNode<>(data);
}
@Override
public String toString() {
return "FourLinkedListNode{" +
"data=" + data +
'}';
}
// 验证
public static void main(String[] args) {
FourLinkedListNode<String> n1 = FourLinkedListNode.create("Node1");
FourLinkedListNode<String> n2 = FourLinkedListNode.create("Node2");
FourLinkedListNode<String> n3 = FourLinkedListNode.create("Node3");
FourLinkedListNode<String> n4 = FourLinkedListNode.create("Node4");
n1.setRight(n2);
n2.setLeft(n1);
n3.setRight(n4);
n4.setLeft(n3);
// 垂直链接
n1.setDown(n3);
n3.setUp(n1);
n2.setDown(n4);
n4.setUp(n2);
// 打印结构
System.out.println("四项链表节点:");
System.out.println(n1 + " right-> " + n1.getRight());
System.out.println(n1 + " down-> " + n1.getDown());
System.out.println(n4 + " left-> " + n4.getLeft());
System.out.println(n4 + " up-> " + n4.getUp());
}
}
/*
jarry@Mac linked % java FourLinkedListNode.java
四项链表节点:
FourLinkedListNode{data=Node1} right-> FourLinkedListNode{data=Node2}
FourLinkedListNode{data=Node1} down-> FourLinkedListNode{data=Node3}
FourLinkedListNode{data=Node4} left-> FourLinkedListNode{data=Node3}
FourLinkedListNode{data=Node4} up-> FourLinkedListNode{data=Node2}
*/