java单链表 提供增删改查_Java实现单向链表的增删改查

class List{private classNode

{privateT data;privateNode next;privateNode(T data)

{if(data != null)this.data =data;

}private voidadd(T data)

{if(this.next == null)this.next = newNode(data);else

this.next.add(data);

}private void remove(Node previous, intindex)

{if(List.this.foot++ ==index)

{

previous.next= this.next;this.next = null;

List.this.count--;return;

}else{this.next.remove(this,index);

}

}private voidremove(Node previous, T data)

{if(this.data.equals(data))

{

previous.next= this.next;this.next = null;

List.this.count--;return;

}if(this.next != null)this.next.remove(this,data);else

return;

}private void replace(intindex, T data)

{if(List.this.foot++ ==index)this.data =data;else

this.next.replace(index,data);

}private voidreplace(T oldData, T newData)

{if(this.data.equals(oldData))this.data =newData;else

this.next.replace(oldData,newData);

}private T get(intindex)

{if(List.this.foot++ ==index)return this.data;else

return this.next.get(index);

}privateboolean contains(T data)

{if(this.data.equals(data))return true;if(this.next != null)return this.next.contains(data);else

return false;

}

}//===============================================

privateNode root;private intcount;private intfoot;publicList()

{}publicboolean isEmpty()

{if(this.count == 0 && this.root == null)return true;else

return false;

}public intsize()

{return this.count;

}public voidadd(T data)

{if(this.isEmpty())this.root = newNode(data);else

this.root.add(data);this.count++;

}public void remove(intindex)

{if(this.isEmpty())return;if(index < 0 || this.count <=index)return;if(index == 0)

{

Node temp= this.root;this.root = this.root.next;

temp.next= null;this.count--;return;

}else{this.foot = 0;this.root.remove(this.root, index);

}

}public voidremove(T data)

{if(this.isEmpty())return;if(this.root.data.equals(data))

{

Node temp= this.root;this.root = this.root.next;

temp.next= null;this.count--;return;

}else{this.root.next.remove(this.root, data);

}

}public void replace(intindex, T data)

{if(this.isEmpty())return;if(index < 0 || this.count<=index)return;this.foot = 0;this.root.replace(index,data);

}public voidreplace(T oldData, T newData)

{if(this.isEmpty())return;this.root.replace(oldData,newData);

}public T get(intindex)

{if(this.isEmpty())return null;this.foot = 0;return this.root.get(index);

}publicboolean contains(T data)

{if(this.isEmpty())return false;return this.root.contains(data);

}publicObject[] toArray()

{if(this.isEmpty())return null;int count = this.count;

Object[] retVal= newObject[count];for(int i = 0; i < count; i++)

{

retVal[i]= this.get(i);

}returnretVal;

}

}public classHello

{public static voidmain(String[] args) throws Exception

{

List myList = new List();

myList.add("Hello");

myList.add("world");

myList.add("Ni");

myList.add("Hao");

myList.add("HeHe");

System.out.println(myList.contains(null));

Object[] result=myList.toArray();for(Object item : result)

{

String temp=(String)item;

System.out.println(temp);

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值