//单链表类,写明方法
public class SinglyLinkedList<T>{
public class Node<T>{
public T data;
public Node<T>next;
public Node(T data,Node<T>next){
this.data=data;
this.next=next;
}
public Node(){
this(null,null);
}
}
public Node<T>head;
public SinglyLinkedList(){
this.head=new Node<T>();
}
//由指定数组中的多个对象构建单链表,采用尾插入构建单链表
public SinglyLinkedList(T[] element){
this();
Node<T>rear=this.head;
for(int i=0;i<element.length;i++){
rear.next=new Node<T>(element[i],null);
rear=rear.next;
}
}
public boolean isEmpty(){
return this.head.next==null;
}
public int length(){
int i=0;
Node<T> p=this.head.next;
while(p!=null){
i++;
p=p.next;
}
return i;
}
public String toString(){
String str="(";
Node<T>p=this.head.next;
while(p!=null){
str +=p.data.toString();
if(p.next!=null)
str +=",";
p=p.next;
}
return str+")";
}
public T get(int i){
if(i>=0){
Node<T>p=this.head.next;
for(int j=0;p!=null&&j<i;j++)
p=p.next;
if(p!=null)
return p.data;
}
return null;
}
public void set(int i,T x){
if(x==null)
return;
if(i>=0){
Node<T>p=this.head.next;
for(int j=0;p!=null&&j<i;j++)
p=p.next;
if(p!=null)
p.data=x;
}
else throw new IndexOutOfBoundsException(i+"");
}
public void insert(int i,T x){
if(x==null)
return;
Node<T>p=this.head;
for(int j=0;p.next!=null&&j<i;j++)
p=p.next;
p.next=new Node<T>(x,p.next);
}
public void append(T x){
insert(Integer.MAX_VALUE,x);
}
//移去首次出现的指定对象
public boolean remove(T key){
boolean test=false;
if(this.head.next==null||key==null)
return test;
Node<T>front=this.head,p=front.next;
while(p!=null&&!p.data.equals(key)){
front=p;
p=p.next;
}
if(p!=null&&p.data.equals(key)){
front.next=p.next;
test=true;
}
if(p!=null){
front.next=p.next;
test=true;
}
return test;
}
public void removeAll(int i){
this.head.next=null;
}
//若找到指定对象,就返回结点,否则返回null
public Node<T> search(T key){
if(key==null)
return null;
Node<T>p=this.head.next;
while(p!=null){
if(p.data.equals(key))
return p.next;
p=p.next;
}
return null;
}
//以查找结果判断单链表是否包含指定对象
public boolean contain(T key){
return this.search(key)!=null;
}
//将单链表中的obj对象替换为对象key
public boolean replace(Object obj,T key){
boolean test=false;
if(this.head.next==null||key==null)
test=false;
Node<T>p=this.head.next;
while(p!=null){
if(p.data.equals(obj)){
p.data=key;
test=true;
}
p=p.next;
}
return test;
}
//以单链表list构造新的单链表,复制单链表
public SinglyLinkedList(SinglyLinkedList list){
this();
Node<T>p=list.head.next;
Node<T>rear=this.head;
while(p!=null){
rear.next=new Node<T>(p.data,null);
rear=rear.next;
p=p.next;
}
}
//将指定单链表list链接在当前单链表之后
public void concat(SinglyLinkedList list){
Node<T>p=this.head;
Node<T>rear=this.head.next;
while(rear.next!=null){
p=rear;
rear=rear.next;
}
rear.next=list.head.next;
}
//比较两条单链表是否相等
public boolean equals(Object obj){
if(obj==this)
return true;
if(!(obj instanceof SinglyLinkedList))
return false;
Node<T>p=this.head.next;
Node<T>q=((SinglyLinkedList<T>)obj).head.next;
while(p!=null&&q!=null&&p.data.equals(q.data)){
p=p.next;
q=q.next;
}
return p==null&&q==null;
}
}
//单链表类调试,调用单链表的方法
public class SinglyLinkedListTest<T> extends SinglyLinkedList<T> {
public static void main(String[] args) {
Integer[] a={1,2,3,4,5,7,8,9,10};
SinglyLinkedList<Integer>list1=new SinglyLinkedList<Integer>(a);
System.out.println("list1单链表:"+list1.toString());
SinglyLinkedList<Integer>list2=new SinglyLinkedList<Integer>(list1);
System.out.println("复制的list1单链表命名为list2单链表:"+list2.toString());
Integer[] b={11,12,13,14,15,16};
SinglyLinkedList<Integer>list=new SinglyLinkedList<Integer>(b);
System.out.println("list单链表:"+list.toString());
list.concat(list1);
System.out.println("list单链表与list1单链表链接在一起:"+list.toString());
boolean a1=list1.equals(list);
System.out.println("判断list1单链表与list单链表是否相等? "+a1);
boolean a2=list1.contain(4);
System.out.println("判断list1单链表是否含有4? "+a2);
boolean a3=list1.replace(4,90);
System.out.println("将list1的对象4替换成对象90是否成功? "+a3);
System.out.println("替换后的list1单链表的全部对象:"+list1.toString());
System.out.println("查询list1单链表的对象1的结点位置:"+list1.search(1).toString());
list1.append(90);
System.out.println("插入对象90后的list1单链表的全部对象:"+list1.toString());
boolean a4=list1.remove(90);
System.out.println("是否删除list1单链表的对象90成功? "+a4);
System.out.println("删除对象90后的list1单链表的全部对象:"+list1.toString());
boolean a5=list1.remove(10);
System.out.println("是否删除list1单链表的对象10成功? "+a5);
System.out.println("删除对象10后的list1单链表的全部对象:"+list1.toString());
}
}