//链表简单形式、
/*class Node{
private Object obj;//存放的真正对象内容
private Node next;//存放的指向
public Node(Object obj){
this.obj=obj;
}
public void setNext(Node next){
this.next=next;
}
public Node getNext(){
return this.next;
}
public void setObject(Object object){
this.obj=object;
}
public Object getObject(){
return this.obj;
}
}
public class TestLinkDemo {
public static void main(String[] args) {
Node root =new Node("火车头");
Node n1 =new Node("车厢A");
Node n2 =new Node("车厢B");
Node n3 =new Node("车厢C");
root.setNext(n1);
n1.setNext(n2);
n2.setNext(n3);
print(root);
}
public static void print(Node node){
if(node!=null){
System.out.print(node.getObject()+"-->");
print(node.getNext());
}
}
}
*/
class Link{//负责链表的操作
// 定义一个内部类,表示Node 类只为Link类服务
private class Node{ //负责数据和链表关系的匹配
private Object data; //存放数据
private Node next; //匹配下一个node
public Node(Object data){
this.data=data;
}
public void addNode(Node newNode){//处理节点关系
if(this.next==null){ //表示当前节点的下一个指向为空,则可以添加
this.next=newNode;
}else{// 迭代直到找到一个为空的进行增加
this.next.addNode(newNode);
}
}
//第一次调用 this=Link.root
//第一次调用 this=Link.root.next
public void toArrayNode(){
Link.this.retData[Link.this.foot++]=this.data;
if(this.next!=null){
this.next.toArrayNode();
}
}
//第一次调用 this=Link.root
//第一次调用 this=Link.root.next
public boolean containsNode(Object search){
if(search.equals(this.data)){
return true;
}
else{
if(this.next!=null){//判断是否到最后一个
return this.next.containsNode(search);
}else{
return false;
}
}
}
//第一次调用 this=Link.root
//第一次调用 this=Link.root.next
//获取索引
public Object getNode(int index){
if(Link.this.foot++==index){
return this.data;
}else{
this.next.getNode(index);
}
return null;
}
public void setNode(int index,Object newData){
if(Link.this.foot++==index){
this.data=newData;
}else{
if(this.next!=null){
this.next.setNode(index,newData);
}
}
}
//第一次调用 this=Link.root.next, previous=Link.root
//第一次调用 this=Link.root.next.next previous=Link.root.next
public void removeNode(Node previous,Object data){
if(this.data.equals(data)){
previous.next=this.next;
}else{
this.next.removeNode(this,data);
}
}
}
//以下为link类代码实现过程------------------
private int count=0;//数据追加次数
private Object[] retData;//返回类型
private int foot=0;//数组下标
private Node root;
public void add(Object data){
if(data == null)return ; // 如果当前数据对象为空,
//处理指向操作
Node newNode=new Node(data);
if(this.root==null){
this.root=newNode;//如果没有根节点,将当前添加的节点设为根节点
}else{//根节点已经存在,需要判断
this.root.addNode(newNode);
}
this.count++;
}
//获取链表长度
public int getSize(){
return this.count;
}
//判断链表是否为空
public boolean isEmpty(){
return this.root==null&&this.count==0;
}
//返回内容
public Object[]toArray(){
if(this.count==0){
return null;
}
else{
//现在链表中有数据,则开辟指定长度的数组
//该数组要交给Node类进行处理
this.retData=new Object[this.count];
this.foot=0;
this.root.toArrayNode();
return this.retData;
}
}
//查询内容
public boolean contains(Object search){
if(search==null||this.root==null){
return false;
}else{
return this.root.containsNode(search);
}
}
//取得指定索引数据
public Object get(int index){
if(index>=this.count){//超过保存数据
return null;
}
this.foot=0;
return this.root.getNode(index);
}
//修改索引数据
public void set(int index,Object newData){
if(index>=this.count){//超过保存数据
return;
}
this.foot=0;
this.root.setNode(index,newData);
}
//删除数据
public void remove(Object data){
if(this.contains(data)){
//判断是否存在
if(this.root.data.equals(data)){//内部类直接访问data变量,首先需要判断要删除的数据是否为根节点数据
this.root=this.root.next;//更改根节点为下一个节点
}else{//不是根节点
this.root.removeNode(this.root,data);
}
this.count--;
}
}
}
public class TestLinkDemo {
public static void main(String[] args) {
Link all=new Link();
all.add("leo");
all.add("jack");
all.add("lims");
all.set(1,"你好");
all.remove("leo");
all.remove("lims");
Object result[]=all.toArray();
for (int x=0;x<result.length ;x++ ){
System.out.println(result[x]);
}
System.out.println("================查询链表是否包含=============");
System.out.println(all.contains("world")?"找到了":"没找到");
System.out.println(all.contains("xxx")?"找到了":"没找到");
System.out.println(all.get(0));
System.out.println(all.get(3));
}
}