java实现单链表

package cc.apl330.sf;

public class Node_ {
	
	private Object data ;
	private Node_ next ;
	
	public Node_() {
		
	}
	
	public Node_(Object data, Node_ next) {
		super();
		this.data = data;
		this.next = next;
	}
	public Node_(Object data) {
		// TODO Auto-generated constructor stub
		this(data,null) ;
	}

	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	public Node_ getNext() {
		return next;
	}
	public void setNext(Node_ next) {
		this.next = next;
	}
	
}
package cc.apl330.sf;
public class NoList {
 private Node_ firstNode ;
 private int length = 0 ;
 
 public NoList() {
 }
 public NoList(Node_ firstNode, int length) {
  super();
  this.firstNode = firstNode;
  this.length = length;
 }
 
 public boolean contains(Object anObject){
  for(int i=0; i<this.length-1; i++){
   if(getNodeAt(i).getData().equals(anObject)){
    return true ;
   }
  }
  return false ;
 }
 
 public void clear(){
  if(isEmpty()) return ;
  Node_ temp = this.firstNode ;
  Node_ temp1 = temp.getNext() ;
  for(int i=0; i<this.length-1; i++){
   temp = null ;
   temp = temp1 ;
   temp1 = temp1.getNext() ;
  }
  
  this.length = 0 ;
 }
 
 @SuppressWarnings("unused")
 public void remove(int index){
  if(!indexValid(index)){
   System.out.println("索引不正确");
   return ;
  }else{
   Node_ before = getNodeAt(index-1) ;
   Node_ after = getNodeAt(index+1) ;
   Node_ now = getNodeAt(index) ;
   if(null == before) {
    this.firstNode = this.firstNode.getNext() ;
    now = null ;
    this.length-- ;
    return  ;
    
   }
   if(null == after){
    before.setNext(null) ;
    now = null ;
    this.length-- ;
    return ;
    
   }
   before.setNext(after);
   now = null ;
   this.length-- ;
   
  }
  
 }
 public void insertAfter(Object data,int index) {
  Node_ newNode = new Node_(data) ;
  if(index <= this.length && index >= 0 && !isEmpty()){
   newNode.setNext(getNodeAt(index+1)) ;
   getNodeAt(index).setNext(newNode) ;
   
  }
  this.length++ ;
 }
 
 public void printAll() {
  if(isEmpty()){
   System.out.println("单链表为空");
   return ;
  }
  Node_ node = this.firstNode ;
  int i = 0 ;
  while(node != null){
   System.out.println( i + " " +  node.getData());
   i++ ;
   node = node.getNext() ;
   
  }
 }
 public boolean indexValid(int index){
  if(index>=0 && index<=this.length && !isEmpty()) return true ;
  return false ;
  
 }
 
 public void addLast(Object data) {
  if(isEmpty()){
   this.firstNode = new Node_(data,null) ;
   this.length++ ;
  }else{
   Node_ newNode = new Node_(data,null) ;
   Node_ node = this.firstNode ;
   while(node.getNext()!=null){
    node = node.getNext() ;
    
   }
   node.setNext(newNode) ;
   this.length++ ;
  }
  
 }
 
 public Node_ getNodeAt(int index) {
  Node_ node = null ;
  if(index >= 0 && index <= this.length && !isEmpty()) {
   node = this.firstNode ;
   for(int i=0; i<index; i++){
    node = node.getNext() ;
    
   }
  }
  return node ;  
  
 }
 
 public boolean isEmpty() {
  return length > 0 ? false : true ;
  
 }
 
 public int getLength() {
  return this.length ;
 }
 
 public static void main(String[] args) {
  NoList nl = new NoList() ;
  nl.addLast("the first ") ;
  nl.addLast("the second ") ;
  nl.addLast("the thirst ") ;
  nl.addLast("the force ") ;
  nl.insertAfter("pp", 2) ;
  nl.printAll() ;
  nl.remove(4) ;
  System.out.println(nl.getLength());
  System.out.println("-----after-delete---------");
  nl.printAll() ;
  System.out.println("------------contains--------");
  System.out.println(nl.contains("the first "));
  nl.clear() ;
  System.out.println("-----after---clear----------");
  nl.printAll() ;
 }
}

你可能感兴趣的:(java实现单链表)