首先建立介面聲明:聲明單鏈表的public方法,介面聲明如下:
public interface IMyList<T>{
public boolean add(T entry);
public boolean add(int aPosition,T entry);
public T remove(int aPosition);
public boolean contains(T entry);
public boolean replace(int aPosition,T entry);
public int getLength();
public boolean isFull();
public boolean isEmpty();
}
然後定義鏈表使用的節點類型,Node類,代碼如下:
class Node<T>{
private T data;
private Node<T> next;
public Node(){}
public Node(T anEntry){
data=anEntry;
}
public T getData(){
return data;
}
public Node<T> getNext(){
return next;
}
public void setData(T entry){
data=entry;
}
public void setNext(Node<T> node){
next=node;
}
}
最後實現單鏈表類,並進行插入測試,代碼如下
public class MyList<T> implements IMyList<T>{
private int length;
private Node<T> firstNode;
public MyList(){
clear();
}
private final void clear(){
length=0;
}
public boolean add(T entry){
Node<T> newNode=new Node<T>(entry);
if(!isEmpty()){
Node<T> lastNode=getNodeAt(length-1);
lastNode.setNext(newNode);
length++;
return true;
}else{
firstNode=newNode;
length++;
return true;
}
}
public boolean add(int aPosition,T entry){
Node<T> newNode=new Node<T>(entry);
if(aPosition==0){
newNode.setNext(firstNode);
//firstNode.setNext(newNode);
firstNode=newNode;
length++;
return true;
}
if(!isEmpty()&&aPosition<length){
Node<T> NodeBefor=getNodeAt(aPosition-1);
Node<T> NodeAfter=NodeBefor.getNext();
NodeBefor.setNext(newNode);
newNode.setNext(NodeAfter);
length++;
return true;
}
return false;
}
public T remove(int aPosition){
Node<T> currentNode=firstNode;
if(aPosition==0){
currentNode=firstNode;
firstNode=firstNode.getNext();
//return firstNode.getData();
}else{
int index=1;
Node<T> beforNode=firstNode;
while(index<aPosition-1&&beforNode!=null){
beforNode=beforNode.getNext();
index++;
}
currentNode=beforNode.getNext();
beforNode.setNext(currentNode.getNext());
}
length--;
return currentNode.getData();
}
public boolean contains(T entry){
Node<T> currentNode=firstNode;
int index=0;
boolean find=false;
while(index<length&&!find&¤tNode!=null){
if(currentNode.getData().equals(entry)){
find=true;
}
}
return find;
}
public boolean replace(int aPosition,T entry){
boolean done=false;
int index=0;
Node<T> currentNode=firstNode;
while(!done&&index<length&¤tNode!=null){
if(currentNode.getData().equals(entry))
done=true;
currentNode=currentNode.getNext();
index++;
}
return done;
}
public int getLength(){
return this.length;
}
private Node<T> getNodeAt(int aPosition){
Node<T> currentNode=firstNode;
for(int index=0;index<aPosition;++index){
currentNode=currentNode.getNext();
}
return currentNode;
}
public boolean isEmpty(){
return (length==0);
}
public boolean isFull(){
return false;
}
public void display(){
Node<T> currentNode=firstNode;
int index=0;
while(currentNode!=null&&index<length){
System.out.println(currentNode.getData());
currentNode=currentNode.getNext();
index++;
}
}
public static void main(String[] args){
MyList<String> lst=new MyList<String>();
lst.add("sunzhenxing");
lst.add(0, "tongji");
lst.add("sunhailong");
lst.display();
System.out.println("=============");
lst.remove(0);
lst.display();
System.out.println("=============");
lst.remove(0);
lst.display();
}
}