JAVA单链表的实现-不带头结点且没有尾指针

时间:2024-01-13 12:03:20

本程序采用JAVA语言实现了线性表的链式实现。首先定义了线性表的接口ListInterface,然后LList类实现了ListInterface完成了链表的实现。

本实现中,链表是不带表头结点的,且有一个指针始终指向链表中的第一个元素,并没有定义尾指针。因此,每次向链表中插入新结点时需要遍历链表一次。

更详细的解释参考《数据结构与算法分析 JAVA语言描述第二版》Frank M. Carrano 著

ListInterface接口的定义如下:

public interface ListInterface<T> {
public boolean add(T newEntry);
public boolean add(int givenPosition, T newEntry);
public void clear();
public T remove(int givenPosition);
public boolean replace(int givenPosition, T newEntry);
public T getEntry(int givenPosition);
public boolean contains(T anEntry);
public int getLength();
public boolean isEmpty();
public void display();
}

具体的实现类LList定义如下:

public class LList<T> implements ListInterface<T>{

    private Node firstNode;//指向第一个结点的指针,该链表是不带头结点的单链表
private int length;//表示单链表的长度 //Node类中不需要定义访问属性的get方法以及set方法,因为Node是内部类,内部类的属性可以直接在外部类中被访问
class Node{
//Node是内部类,其外部类中已经定义了T,故可以在这里使用通配符T
private T data;//结点的数据部分
private Node next;//结点的指针部分,指向下一个结点
//Node类中不需要默认构造器
public Node(T dataPortion){
data = dataPortion;
}
public Node(T dataPortion, Node nextNode){
data = dataPortion;
next = nextNode;
}
} public LList(){
clear();
}
//获取链表中指定位置处的结点
private Node getNodeAt(int givenPosition){
assert (!isEmpty() && ((1 <= givenPosition) && (givenPosition <= length)));
Node currentNode = firstNode;
for(int counter = 1; counter < givenPosition; counter++){
currentNode = currentNode.next;
}
assert currentNode != null;
return currentNode;
} @Override
public boolean add(T newEntry) {
// 将每个新结点插入到链表的末尾,通过getNodeAt()方法来获得最后一个元素的地址
Node newNode = new Node(newEntry);
if(isEmpty()){//插入第一个结点
firstNode = newNode;
}
else{//在其它位置插入结点
Node lastNode = getNodeAt(length);//这里每插入一个元素都需要遍历一次链表,代价较大
lastNode.next = newNode;
}
length++;
return true;
} @Override
public boolean add(int givenPosition, T newEntry){//在指定位置处插入结点
boolean isSuccessful = true;
if(givenPosition >= 1 && givenPosition <= length + 1){
Node newNode = new Node(newEntry);
if(isEmpty() || givenPosition == 1){//在第一个位置处插入结点
newNode.next = firstNode;
firstNode = newNode;
}
else{//在其它位置插入结点
Node nodeBefore = getNodeAt(givenPosition - 1);
Node nodeAfter = nodeBefore.next;
nodeBefore.next = newNode;
newNode.next = nodeAfter;
}
length++;
}
else
isSuccessful = false;
return isSuccessful;
} @Override
public final void clear() {//clear()在构造器中被调用了,所以用final修饰
firstNode = null;
length = 0;
} @Override
public T remove(int givenPosition) {//删除指定位置处的结点
T result = null;
if((!isEmpty()) && ((givenPosition >= 1) && (givenPosition <= length))){
if(givenPosition == 1){//删除第一个位置处的结点
result = firstNode.data;
firstNode = firstNode.next;
}
else//删除表中其它位置结点
{
Node nodeBefore = getNodeAt(givenPosition - 1);
Node nodeToRemove = nodeBefore.next;
Node nodeAfter = nodeToRemove.next;
nodeBefore.next = nodeAfter;
result = nodeToRemove.data;
}
length--;
}
return result;
} @Override
public boolean replace(int givenPosition, T newEntry) {//替换指定位置处结点的值
boolean isSuccessful = true;
if((!isEmpty()) && ((givenPosition >= 1) && (givenPosition <= length))){
Node desireNode = getNodeAt(givenPosition);
desireNode.data = newEntry;
}
else
isSuccessful = false;
return isSuccessful;
} @Override
public T getEntry(int givenPosition) {//获取指定位置的结点的值
T result = null;
if((!isEmpty()) && ((givenPosition >= 1) && (givenPosition <= length))){
result = getNodeAt(givenPosition).data;
}
return result;
} @Override
public boolean contains(T anEntry) {//判断链表中的结点是否包含某个值
boolean found = false;
Node currentNode = firstNode;
while(!found && currentNode != null){
if(currentNode.data.equals(anEntry)){
found = true;
break;
}
currentNode = currentNode.next;
}
return found;
} @Override
public int getLength() {//获取链表的长度
return length;
} @Override
public boolean isEmpty() {//判断链表是否为空
boolean result;
if(length == 0){
assert firstNode == null;
result = true;
}
else{
assert firstNode != null;
result = false;
}
return result;
} @Override
public void display() {//遍历链表,显示链表中的每个结点的值
Node currentNode = firstNode;
while(currentNode != null){
System.out.println(currentNode.data);
currentNode = currentNode.next;
}
}
}