java没有指针怎么处理链表_JAVA单链表的实现-不带头结点且没有尾指针

public class LList implements ListInterface{private Node firstNode;//指向第一个结点的指针,该链表是不带头结点的单链表

private int length;//表示单链表的长度//Node类中不需要定义访问属性的get方法以及set方法,因为Node是内部类,内部类的属性可以直接在外部类中被访问

classNode{//Node是内部类,其外部类中已经定义了T,故可以在这里使用通配符T

private T data;//结点的数据部分

private Node next;//结点的指针部分,指向下一个结点//Node类中不需要默认构造器

publicNode(T dataPortion){

data=dataPortion;

}publicNode(T dataPortion, Node nextNode){

data=dataPortion;

next=nextNode;

}

}publicLList(){

clear();

}//获取链表中指定位置处的结点

private Node getNodeAt(intgivenPosition){assert (!isEmpty() && ((1 <= givenPosition) && (givenPosition <=length)));

Node currentNode=firstNode;for(int counter = 1; counter < givenPosition; counter++){

currentNode=currentNode.next;

}assert currentNode != null;returncurrentNode;

}

@Overridepublic booleanadd(T newEntry) {//将每个新结点插入到链表的末尾,通过getNodeAt()方法来获得最后一个元素的地址

Node newNode = newNode(newEntry);if(isEmpty()){//插入第一个结点

firstNode =newNode;

}else{//在其它位置插入结点

Node lastNode = getNodeAt(length);//这里每插入一个元素都需要遍历一次链表,代价较大

lastNode.next =newNode;

}

length++;return true;

}

@Overridepublic boolean add(int givenPosition, T newEntry){//在指定位置处插入结点

boolean isSuccessful = true;if(givenPosition >= 1 && givenPosition <= length + 1){

Node newNode= newNode(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++;

}elseisSuccessful= false;returnisSuccessful;

}

@Overridepublic final void clear() {//clear()在构造器中被调用了,所以用final修饰

firstNode = null;

length= 0;

}

@Overridepublic 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--;

}returnresult;

}

@Overridepublic boolean replace(int givenPosition, T newEntry) {//替换指定位置处结点的值

boolean isSuccessful = true;if((!isEmpty()) && ((givenPosition >= 1) && (givenPosition <=length))){

Node desireNode=getNodeAt(givenPosition);

desireNode.data=newEntry;

}elseisSuccessful= false;returnisSuccessful;

}

@Overridepublic T getEntry(int givenPosition) {//获取指定位置的结点的值

T result = null;if((!isEmpty()) && ((givenPosition >= 1) && (givenPosition <=length))){

result=getNodeAt(givenPosition).data;

}returnresult;

}

@Overridepublic 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;

}returnfound;

}

@Overridepublic int getLength() {//获取链表的长度

returnlength;

}

@Overridepublic boolean isEmpty() {//判断链表是否为空

booleanresult;if(length == 0){assert firstNode == null;

result= true;

}else{assert firstNode != null;

result= false;

}returnresult;

}

@Overridepublic void display() {//遍历链表,显示链表中的每个结点的值

Node currentNode =firstNode;while(currentNode != null){

System.out.println(currentNode.data);

currentNode=currentNode.next;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值