通过java实现较为简单的单向链表数据结构 //节点类===============================================package link;/** * * Title:Node * Description:节点 * Company: *@author mym *@date 2017-7-9下午7:41:16 *@version */public c
//节点类=============================================== package link; /** * *Title:Node
*Description:节点
*Company:
*@author mym *@date 2017-7-9下午7:41:16 *@version */ public class Node { public long data; //节点数据 public Node next; //下一个节点 public Node(long value){ this.data = value; } /** * *title:display
*function:显示当前表头数据
*@author mym */ public void display(){ System.out.print(data + " "); } } ============================================================================= 链表: package link; /** * *Title:LinkList
*Description:相当于火车(包含多个火车节点)
*Company:
*@author mym *@date 2017-7-9下午7:44:18 *@version */ public class LinkList { public Node first; //只关注的链表头头节点 public LinkList(){ first = null; //初始化 } /** * 插入节点 */ public void insertNode(long value){ Node node = new Node(value); node.next = first; //插入的节点的next指向当前存在的头节点 first = node; //把头节点指针指向刚插入的节点 } /** * 移出节点 */ public long deleteNode(){ Node currentFirst = first; if(first != null){ first = first.next; //把头节点指针后移 } return currentFirst.data; } /** * 显示所有 */ public void display(){ Node currentNode = first; while(currentNode != null){ currentNode.display(); currentNode = currentNode.next; //指针后移一位 } System.out.println(); //换行 } /** * 根据数据查询节点 */ public Node selectNode(long value){ Node currentNode = first; //假设是头节点 while(currentNode != null && currentNode.data != value){ currentNode = currentNode.next; } return currentNode; } /** * 根据数据删除节点 */ public void deleteNode(long value){ Node deleteNode = null; Node preNode = first; //当前删除节点的前节点 if(first.data == value){ deleteNode(); //直接调用之前的删除头节点的方法 }else{ deleteNode = first.next; while(deleteNode.data != value){ preNode = deleteNode; deleteNode = deleteNode.next; } preNode.next = deleteNode.next; } } }