public class Node<T> {
T data;
Node<T> next;
public Node(T data, Node<T> next) {
super();
this.data = data;
this.next = next;
}
public Node(T data) {
super();
this.data = data;
}
public Node() {
super();
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public String toString() {
return "Node [data=" + data + ", next=" + next + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((data == null) ? 0 : data.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node<T> other = (Node<T>) obj;
if (data == null) {
if (other.data != null)
return false;
} else if (!data.equals(other.data))
return false;
return true;
}
}
public class Link<T> {
Node<T> head;
public Link() {
super();
head = new Node<T>();
}
public void appand(Node<T> node)
{
if(node==null)return;
Node<T> lp = head;
while(lp.next!=null)
{
lp = lp.next;
}
lp.next = node;
}
public Node<T> find(Node node)
{
Node<T> move = head.next;
while(move!=null)
{
判断当前的move是否等于我们要查找的node
if(move.equals(node))
return move;
move = move.next;
}
return null;
}
public void visitAll()
{
Node<T> lp = head.next;
while(lp!=null)
{
System.out.println(lp.data);
lp = lp.next;
}
}
public void visitAll2()
{
Node<T> lp = head;
while(lp.next!=null)
{
lp = lp.next;
System.out.println(lp.data);
}
}
public void insertFirst(Node<T> node)
{
node.next = head.next;
head.next = node;
}
public void remove(Node<T> node) 1
{
Node<T> prior = head;
Node<T> current= head.next;
while(current!=null)
{
if(current.equals(node))成立 1
{
prior.next = current.next;
return;
}
prior = prior.next;
current = current.next;/后移
}
}
public boolean insert(Node<T> newNode,Node<T> node)
{
Node<T> prior = head;
Node<T> current= head.next;
while(current!=null)
{
if(current.equals(node))
{
newNode.next = current;
prior.next = newNode;
return true;
}
prior = prior.next;
current = current.next;
}
return false;
}
}
public class Main {
public static void main(String[] args) {
Link<Integer> link = new Link<Integer>();
link.insertFirst( new Node<Integer>(1) );
link.insertFirst( new Node<Integer>(2) );
link.insertFirst(new Node<Integer>(3) );
link.insertFirst(new Node<Integer>(4) );
link.insertFirst(new Node<Integer>(5) );
link.remove(new Node<Integer>(1));
link.remove(new Node<Integer>(5));
link.visitAll();
link.insert( new Node<Integer>(5),new Node<Integer>(4));
link.visitAll();
System.out.print(link.find(new Node<Integer>(5)));
System.out.print(link.find(new Node<Integer>(1)));
}
}