package MyLink.MySingleLink;
import java.util.Objects;
/**
*单链表结点类
* */
public class Node {
/**
* 数据域
* */
private Object date;
/**
* 指针域
* */
private Node next;
public Node(Object date){
this.date = date;
}
public Node(Object date,Node next){
this.date = date;
this.next = next;
}
public Object getDate(){
return this.date;
}
public Node getNext(){
return this.next;
}
public void setDate(Object date){
this.date = date;
}
public void setNext(Node next){
this.next = next;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return Objects.equals(date, node.date) &&
Objects.equals(next, node.next);
}
@Override
public int hashCode() {
return Objects.hash(date, next);
}
@Override
public String toString() {
return "Node{" +
"date=" + date +
", next=" + next +
'}';
}
}
----------------
package MyLink.MySingleLink;
import java.util.Objects;
/**
* 带有头结点的单链表
* */
public class Link {
/**
* 头节点
* */
private Node head;
//构造此链表须初始化头节点
public Link(Node node){
head = node;
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Link link = (Link) o;
return Objects.equals(head, link.head);
}
@Override
public int hashCode() {
return Objects.hash(head);
}
@Override
public String toString() {
return "Link{" +
"head=" + head +
'}';
}
/**
* 打印单链表
* */
public void printSingleLink(){
Node pMove = head;
while(pMove != null) {
System.out.print(pMove.getDate() + "-->");
pMove = pMove.getNext();
}
}
/**
* 单链表增加结点 尾插法
* */
public void insertNodeByTail(Node node){
Node pMove = head;
while(pMove.getNext() != null){
pMove = pMove.getNext();
}
pMove.setNext(node);
System.out.println("insert\t"+ node.getDate() +"\t successful!");
}
/**
* 单链表删除结点
* */
public boolean deleteNode(Node node){
//如果没用这个结点 删除失败
if(!findNode(node)){
System.out.println("delete"+ node.getDate() +"fail!");
return findNode(node);
}
//否则执行删除
Node pMove1 = head;
Node pMove2 = head.getNext();
while(pMove1.getNext() != null){
if(pMove2.equals(node)){
pMove1.setNext(pMove2.getNext());
}
pMove1 = pMove2;
pMove2 = pMove1.getNext();
}
System.out.println("delete\t"+ node.getDate() +"\tsuccessful!");
return true;
}
/**
* 单链表查找结点 找到返回true否则false
* */
public boolean findNode(Node node){
Node pMove = head;
while(pMove != null){
if(pMove.equals(node)){
return true;
}
pMove = pMove.getNext();
}
System.out.println("not found "+node.getDate());
return false;
}
}
----------------------
package MyLink.MySingleLink;
/**
* 单链表测试类 程序入口
* */
public class MainT {
public static void main(String[] args){
//创建头结点的单链表
Node head = new Node("head");
Link link = new Link(head);
Node node1 = new Node("first");
Node node2 = new Node("second");
Node node3 = new Node("third");
Node node4 = new Node("four");
Node node5 = new Node("fifth");
Node node6 = new Node("sixth");
link.insertNodeByTail(node1);
link.insertNodeByTail(node2);
link.insertNodeByTail(node3);
link.insertNodeByTail(node4);
link.insertNodeByTail(node5);
link.printSingleLink();
System.out.println();
System.out.println(link.findNode(node6));
link.deleteNode(node5);
}
}
----测试结果---
标签:Node,node,head,单链,java,源码,date,return,public From: https://www.cnblogs.com/pinglu/p/16652458.html