实现一个单链表


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();
		// TODO Auto-generated constructor stub
	}
	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();
      //定义构造方法,头节点为null;
	    head = new Node<T>();
	}
	//循环执行完毕以后,lp.next为null,lp指向最后一个结点
	public void appand(Node<T> node)//传进一个Node类型的 node
	{
 //如果node等于null传进来的数为空 直接返回

		if(node==null)return; 
   //为了防止head里面的数据结构改变,定义一个类型为Node-lp的指针
		Node<T> lp = head;
  //判断第一个节点是否不等于null, lp.next;
		while(lp.next!=null)
		{
      //如果不为null说明lp当前的位置不是最后的位置
      //因为我们要实现一个在尾部添加的方法
			lp = lp.next;//继续后移,调用Node类的next属性!
		}
  //这里能执行是因为lp是最后一个数了lp.next == null说明下一个数为null了
  //就在这个数的后面添加进去就行了
  //lp的下一个指向就指向我们传进来的node 实现一个在尾部添加的方法!
		lp.next = node;
	}
 
 //定义一个查找的方法,返回类型为Node
	public Node<T> find(Node node)
	{
     //定义辅助遍历指针move,将第一个节点赋值给他
		Node<T> move = head.next;
  //判断第一个节点是否为空,不为空则继续
		while(move!=null)
    		{
                  //上面Node类里面老师重写了equals方法,所以这里来用
                  判断当前的move是否等于我们要查找的node
			  if(move.equals(node))
                     //if成立,就然会一个move
				  return move;
                      //否则继续以后,向下查找!
			move = move.next;
		}
  //没有找到,返回null
		return null;
	}
	//循环执行完毕以后,lp为null
	public void visitAll()
	{
		Node<T> lp = head.next;
		while(lp!=null)
		{
			System.out.println(lp.data);
			lp = lp.next;
		}
	}
	//循环执行完毕以后,lp.next为null,lp指向最后一个结点
	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)));
	 

		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值