单链表的实现,定义为SingleLinkedList

class Node<AnyType>{        //结点Node类
public AnyType data;
public Node<AnyType> next;
public Node(AnyType d,Node<AnyType> next ){
this.data=d;
this.next=next;
}
public Node(AnyType d){
this.data=d;
this.next=null;
}
public Node(){
this.data=null;
this.next=null;
} }
public class SingleLinkedList<AnyType extends Comparable<?super AnyType>> {
int theSize;
Node<AnyType> firstNode; //定义第一个结点 public SingleLinkedList(){ //对链表初始化
this.firstNode=null;
theSize=0;
} public int getSize(){ //获取链表长度
int count=0;
if(firstNode==null) return 0;
Node<AnyType> p=firstNode;
while(p!=null){
count++;
p=p.next;
}
return count;
} public Node<AnyType> getNode(int idx){ //获取idx对应的结点
Node<AnyType> p;
if(idx<0||idx>getSize()){ //判断idx的合法性
return null;
}
else{
p=firstNode;
for(int i=0;i<idx;i++){
p=p.next;
}
return p;
}
}
public AnyType setNode(int idx,AnyType newData){ //替换单链表中idx位置的数据
Node<AnyType> p=firstNode;
if(idx<0||idx>getSize())
return null;
else{
for(int i=0;i<idx;i++)
p=p.next;
AnyType oldData=p.data;
p.data=newData;
return oldData;
}
} public boolean add(AnyType x){ //插入元素,未给出idx自动加到表尾
add(getSize(),x);
return true;
}
public void add(int idx,AnyType x){ //将元素加到idx位置
Node<AnyType> newNode=new Node<AnyType>(x);
if(firstNode==null||idx==0){ //空表
newNode.next=firstNode;
firstNode=newNode;
theSize++;
}
else if(idx>theSize+2){ //idx不合法时处理方法
System.out.println("插入失败");
}
else{
Node<AnyType> p=getNode(idx-1);
newNode.next=p.next;
p.next=newNode;
theSize++; }
}
public Boolean remove(int idx){
if(firstNode==null){ //空表
return false;
}
else if(idx==0||firstNode.next==null){ //只有一个结点或idx=0;
Node<AnyType> p=this.firstNode; //定义结点p,将头结点的值赋给p
firstNode=p.next; //令头结点为p的下一个结点,即删除头结点
theSize--;
return true;
} else{
Node<AnyType> p=getNode(idx-1);
Node<AnyType> q=p.next;
p.next=q.next;
theSize--;
return true;
}
} public int contains(AnyType x,SingleLinkedList sll){ //判断x是否在链表中
int idx=0;
Node<AnyType> p=sll.firstNode;
if(sll.firstNode==null) //空表
return -1;
while(p.data!=x){
idx++;
p=p.next;
if(p==null)
return -1;
}
return idx;
} public void moveSmall(){ //将最小元素移到链表最前端
Node<AnyType> small=firstNode;
Node<AnyType> p=firstNode;
int id=0;
for(int i=0;i<theSize;i++){
if((Integer)p.data<(Integer)small.data||(Integer)p.data==(Integer)small.data){
small=p;
id=i;
}
p=p.next;
}
remove(id);
add(0,small.data);
}
public void exchange(int x,int y){ ///交换两个相邻位置的元素
if(x>y){ //使x<y
int temp=x;x=y;y=temp;
}
if(y>getSize()-1){ //输入数据超出范围
System.out.println("数据超出范围,交换失败");
}
else if(getSize()==2){ //单链表只有两个元素
Node<AnyType> p=getNode(1);
p.next=firstNode;
firstNode.next=null;
firstNode=p;
} else if(getSize()>2&&y==getSize()-1){ //元素个数大于二,交换表尾的两个数据 Node<AnyType> p=getNode(x-1);
Node<AnyType> q=p.next;
p.next=q.next;
p.next.next=q;
q.next=null;
} else if(getSize()>2&&x==0){ //元素个数大于二,交换表头的两个数据
Node<AnyType> p=getNode(1);
firstNode.next=p.next;
p.next=firstNode;
firstNode=p;
}
else if(getSize()>3){ //元素个数大于4,交换不挨边的数据
Node<AnyType> p=getNode(x-1);
Node<AnyType> q=getNode(x);
Node<AnyType> r=getNode(y);
p.next=r.next;
q.next=p.next;
r.next=q;
p.next=r; }
} public void merge(SingleLinkedList La,SingleLinkedList Lb){ //把Lb的元素接到La尾
for(int i=0;i<Lb.getSize();i++){
Node<AnyType> p=Lb.getNode(i);
AnyType m=p.data;
La.add(m);
}
}
public static void main(String[] args) {
//验证部分
SingleLinkedList<Integer> La=new SingleLinkedList<Integer>();
SingleLinkedList<Integer> Lb=new SingleLinkedList<Integer>();
La.add(1);
La.add(2);
La.add(3);
La.add(4);
La.add(5);
La.add(6);
Lb.add(11);
Lb.add(12);
Lb.add(13);
System.out.println("长度为:"+La.getSize());
for(int i=0;i<La.getSize();i++){
System.out.println(La.getNode(i).data);
}
System.out.println("........................"); La.setNode(3,0);
La.remove(0);
System.out.println("长度为:"+La.getSize());
for(int i=0;i<La.getSize();i++){
System.out.println(La.getNode(i).data);
}
System.out.println("........................"); La.add(2,9);
System.out.println("长度为:"+La.getSize());
for(int i=0;i<La.getSize();i++){
System.out.println(La.getNode(i).data);
}
System.out.println("........................"); System.out.println("结果:"+La.contains(91,La));
System.out.println("........................"); La.merge(La,Lb);
for(int i=0;i<La.getSize();i++){
System.out.println(La.getNode(i).data);
}
System.out.println("........................"); System.out.println("长度为:"+La.getSize());
La.exchange(1,2);
for(int i=0;i<La.getSize();i++){
System.out.println(La.getNode(i).data);
} }
}
05-11 01:28