java实现单向链表--创建、遍历

来源:互联网 发布:linux系统api有哪些 编辑:程序博客网 时间:2024/06/06 02:38

链表是非常常用的数据结构,数据结构是不分语言的,在此我只是通过Java语言实现了一个极其简陋的单向链表,后续还好实现双向链表,双向循环链表等。

/**

 * java实现单向链表
 * Title: QueueTest.java
 * Copyright: Copyright (c) 2007
 * Company: LTGames
 * @author author
 * @date 2017年4月12日 下午10:30:13
 * @version 1.0
 */
public class QueueTest {


private Entrys entrys; //当前节点
private Entrys head; //头结点

class Entrys{
public Object object;
public Entrys next;

public Entrys(Object object, Entrys next){
this.object = object;
this.next = next;
}
}

/**
* 添加节点
* @param obj
*/
public void add(Object obj){
Entrys entrysNew = new Entrys(obj,null);
if(head == null){
head = entrysNew;
entrys = entrysNew;
} else {
entrys.next = entrysNew; //当前节点指向新的节点
entrys = entrysNew; //当前节点向后移动
}
}

/**
* 遍历节点
* @param entrys
*/
public void findQueue(Entrys entrys){
while(entrys != null){
System.out.println(entrys.object);
entrys = entrys.next;
}
}

public static void main(String[] args){
    QueueTest test = new QueueTest();
    test.add("a");
    test.add("b");
    test.add("c");
    test.add("d");
    test.add("e");
    test.findQueue(test.head);
  }
}
1 0
原创粉丝点击