单向无头节点链表(Java)

前言

与之前写过的单向链表思想一样,只不过换了一种语言,在此练习记录一下

链表接口

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
public interface SListMethod<E> {


//初始化和销毁
void InitSeqList();

void DestroySeqList(SList<E> sList);

//尾插/头插
void PushBackSeqList(E data);

// void PushBackSeqList(SList<E> sList,E data);
void PushFrontSeqList(E data);

//头删/头插
void PopBackSeqList();

void PopFrontSeqList();

//在指定节点位置插入
void InsertSeqList(int pos,E data);

//删除pos这个节点
void EraseSeqList(E data);

//查找
//从第一个开始查找如果找到了返回第一次出现位置的索引,如果没有找到则返回-1
SList<E>.Node FindSeqList(E data);

//删除遇到的第一个data
void RemoveSeqList(E data);

//打印
void PrintSeqList(SList<E> sList);
}

具体实现类

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
public class SList<E> implements SListMethod<E> {

public class Node {
private E data;
private Node next;

public Node() {

}

public Node(E data, Node next) {
this.data = data;
this.next = next;
}
}

private Node head;
private Node tail;
private int size;

@Override
public void InitSeqList() {
head = null;
tail = null;
size = 0;
}

@Override
public void DestroySeqList(SList<E> sList) {
head = null;
tail = null;
size = 0;
}

@Override
public void PushBackSeqList(E data) {
if (head == null) {
head = new Node(data, null);
tail = head;
size++;
} else {
Node newNode = new Node(data, null);
tail.next = newNode;
tail = newNode;
size++;
}
}

@Override
public void PushFrontSeqList(E data) {
if (head == null) {
Node newNode = new Node(data, null);
head = newNode;
tail = head;
size++;
} else {
Node newNode = new Node(data, null);
newNode.next = head;
head = newNode;
size++;
}

}

@Override
public void PopBackSeqList() {
if (head == null) {
System.out.println("该链表已经为空不可进行删除操作");
return;
}

if (head.next == null) {
head = null;
size--;
return;
}

Node cur = head;
while (cur.next.next != null) {
cur = cur.next;
}
cur.next = null;
tail = cur;
size--;
}

@Override
public void PopFrontSeqList() {
if (head == null) {
System.out.println("该链表已经为空不可进行删除操作");
return;
}

Node cur = head.next;
head.next = null;
head = cur;
size--;
}

@Override
public void InsertSeqList(int pos, E data) {
if(pos == 1 || head == null){
PushFrontSeqList(data);
return;
}

int i = 1;
Node cur = head;
while(i<pos-1){
cur = cur.next;
i++;
}

Node newNode = new Node(data,null);
newNode.next = cur.next;
cur.next = newNode;
size++;
}

@Override
public void EraseSeqList(E data) {
Node deletNode = FindSeqList(data);
Node cur = head;
while(cur.next != deletNode){
cur = cur.next;
}
cur.next = deletNode.next;
deletNode.next = null;
size--;
}

@Override
public Node FindSeqList(E data) {
for(Node cur = head; cur != null; cur=cur.next){
if(cur.data == data || cur.data.equals(data)){
return cur;
}
}
System.out.println("所要查找的元素不存在");
return null;
}

@Override
public void RemoveSeqList(E data) {

Node prev = null;
Node cur = head;


while(cur != null && cur.data != data){
prev = cur;
cur = cur.next;
}

prev.next = cur.next;
cur.next = null;
size--;
}

@Override
public void PrintSeqList(SList<E> sList) {
for (Node cur = head; cur != null; cur = cur.next) {
System.out.print(cur.data + " ");
}
System.out.println();
}

//Java中的所有方法传递的都是值,没有传址这一概念,所以这种方式虽然也对,但是看起来会较为繁琐
// @Override
// public void PushBackSeqList(SList<E> sList,E data) {
// if(sList.head == null){
// sList.head = new Node(data,null);
// sList.tail = head;
// }else{
// Node newNode = new Node(data,null);
// sList.tail.next = newNode;
// sList.tail = newNode;
// }
// }
}

谨此在这里记录学习过程~