java 循环单链表 尾指针_JAVA单链表的实现-不带头结点但带有尾指针

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

private Node lastNode;//尾指针,指向链表中的最后一个结点

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;

}

}publicLList2(){

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) {

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

firstNode =newNode;

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

lastNode.next =newNode;

}

lastNode=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()){//表空为,插入某个元素

firstNode =newNode;

lastNode=newNode;

}else if(givenPosition == 1){//表不空时,在第一个位置处插入元素

newNode.next =firstNode;

firstNode=newNode;

}else if(givenPosition == length + 1){//表不空时,在最后一个位置处插入元素

lastNode.next =newNode;

lastNode=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;

lastNode= 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;if(length == 1)//链表中只有一个元素时,删除之后,尾指针为空

lastNode = null;

}else//删除表中其它位置结点

{

Node nodeBefore= getNodeAt(givenPosition - 1);

Node nodeToRemove=nodeBefore.next;

Node nodeAfter=nodeToRemove.next;

nodeBefore.next=nodeAfter;

result=nodeToRemove.data;if(givenPosition == length)//当删除最后一个元素后,尾指针应指向其前一个元素

lastNode =nodeBefore;

}

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值