1
2
import
java.util.ArrayList;
3
import
java.util.List;
4
5
/** */
/**
6
* 链表实现ADT
7
* @author BruceLeey
8
*/
9
class
Node
{
10
11
Object obj; //数值域
12
Node next; //链域
13
14
public Node()
{
15
}
16
17
public Node(Object value)
{
18
19
this.obj = value;
20
next = null;
21
}
22
}
23
24
class
LinkList
{
25
26
private Node first; //头节点,不记录在链表之内
27
private int size; //记录链表节点长度
28
29
public LinkList()
{
30
31
first = null;
32
size = 0;
33
34
}
35
36
/** *//**
37
* 添加节点
38
*
39
* @param value
40
*/
41
public void addNode(Object value)
{
42
System.out.println("\n-------------------------添加节点 " + value + " -------------------------");
43
Node currentNode = new Node(value);
44
currentNode.next = first; //当前节点链域指向头节点
45
first = currentNode; //头节点记录当前节点地址
46
size++;
47
}
48
49
/** *//**
50
* 验证是否为空
51
* @return
52
*/
53
public boolean isEmpty()
{
54
return size == 0;
55
}
56
57
/** *//**
58
* 删除表头
59
* @param value
60
*/
61
public Node removeFirstNode()
{
62
System.out.println("\n-------------------------移除头节点-------------------------");
63
Node temp = first;
64
first = first.next; //指向下一节点
65
size--;
66
System.out.println("\n移除的表头数据为: " + temp.obj);
67
return temp; //返回删除的节点
68
}
69
70
/** *//**
71
* 封装长度
72
* @return
73
*/
74
public int getSize()
{
75
76
return size;
77
}
78
79
/** *//**
80
* 找出索引之前的节点
81
* @param index
82
* @return
83
*/
84
public List<Node> getNodeByIndex(int index)
{
85
System.out.println("\n-------------------------查找" + index + "之前的所有节点-------------------------");
86
List<Node> list = new ArrayList<Node>();
87
assert (!(index > getSize() - 1 || index < 0));
88
Node current = first; //定位到头节点
89
for (int i = 0; i < index; i++)
{
90
list.add(current);
91
current = current.next; //以此往下移
92
}
93
for (int j = 0; j < list.size(); j++)
{
94
System.out.println("\n查找到的数据为: " + list.get(j).obj);
95
}
96
return list;
97
}
98
99
/** *//**
100
* 输出链表
101
*/
102
public void displayNode()
{
103
System.out.println("\n-------------------------开始输出链表-------------------------");
104
assert (!this.isEmpty());
105
Node current = first;
106
for (int i = 0; i < getSize(); i++)
{
107
108
System.out.println("节点为: " + current.obj.toString());
109
current = current.next;
110
}
111
112
}
113
}
114
115
116
public
class
TestAdt
{
117
118
public static void main(String[] args)
{
119
LinkList link = new LinkList();
120
for (int i = 0; i < 10; i++)
{
121
link.addNode("我是节点 " + i);
122
}
123
link.displayNode();
124
Node node = link.removeFirstNode();
125
link.displayNode();
126
link.getNodeByIndex(5);
127
link.displayNode();
128
129
}
130
}
131

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

110

111

112

113

114

115

116



117

118



119

120



121

122

123

124

125

126

127

128

129

130

131
