JAva-単一チェーンテーブル

4905 ワード

package chain;

public class Node {

	// 0       
	private Node next;
	
	private int data;
	
	// 1     
	public Node(int data) {
		this.data = data;
	}
	
	// 2   
	public void display(){
		System.out.println(data+ " ");
	}
	
	public int display1(){
		return data;
	}
	
	

	/**
	 * @return the next
	 */
	public Node getNext() {
		return next;
	}

	/**
	 * @param next the next to set
	 */
	public void setNext(Node next) {
		this.next = next;
	}

	/**
	 * @return the data
	 */
	public int getData() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public void setData(int data) {
		this.data = data;
	}
	
	
	
}

  
 
 
 
package chain;

/**
 *    
 * @author zm
 *
 */
public class LinkList {

	// 0     
	private Node head;
	private Node tail;
	private int size;
	// 1    
	public LinkList(){
		this.head = null;
		this.tail = null;
		this.size = 0;
	}
	public LinkList(int data){
		this.head = new Node(data);
		this.tail = null;
		this.size = 1;
	}
	
	// 2  
	
	// 2.1   
	public void add(int data){
		
		Node node = new Node(data);
		if(this.head == null){
			this.head = node;
			this.tail = node;
		}else{
			this.tail.setNext(node);
			this.tail = node;
		}
		this.size ++;
	}
	
	// 2.2    
	public void clear(){
		this.head = null;
		this.tail = null;
		System.gc();
	}
	
	// 2.3      
	public int deleteLast(){
		// 0          
		// 1         
		// 2                 
		Node point = this.head;
		while(point.getNext() != this.tail) {
			point = point.getNext();
		}
		tail = point;
		tail.setNext(null);
		size--;
		return 0;
	}
	// 2.4         (    、        )
	public boolean delete(int location){
		if(location >= this.size -1 || location <= 0){
			System.out.println("out of range");
			return false;
		}
		
		boolean result = false;
		int count = 0;
		Node node = this.head;
		Node priveos = null;
		while(node.getNext() != this.tail){ //               
			if(count == location-1){
				System.out.println("find the data, the previos location is " + (location-1));
				System.out.println("the previos data is " + node.display1());
				priveos = node;
				result = true;
				size--;
				break;
			}
			node = node.getNext();
			count++;
		}	
		if(result){
			Node dealNode = priveos.getNext();
			Node dealNextNode = dealNode.getNext();
			priveos.setNext(dealNextNode);
			dealNode.setNext(null);
		}
		return result;
	}
	
	// 2.5         
	public boolean exists(int data){
		boolean flag = false;
		Node point = head;
		while(point.getNext() != null) {//          
			if(point.getData() == data){
				flag = true;
				break;
			}
			point = point.getNext();
		}
		if(!flag){ //         
			if(tail.getData() == data) {
				flag = true;
			}
		}
		return flag;
	}
	// 2.6       
	public void display(){
		Node node = head;
		while(node.getNext() != null){//          
			 System.out.println(node.getData());
			 node = node.getNext();
		}
		System.out.println(tail.getData());//          
	}
	// 2.7      
	public void deleteHead(){
		
		if(this.size ==1){
			head = null;
			tail = null;
			size = 0;
		}else{
			Node node = head.getNext();
			head.setNext(null);
			head = node;
			
		}
	}

	
	
	public int size() {  
        return this.size;  
    }  
	
	public static void main(String[] args) {
		
		LinkList list = new LinkList();
		list.add(1);  
        list.add(2);  
        list.add(3);  
        list.add(4);  
        list.add(5);
        //System.out.println(list.size());
        //list.display();
        
        //list.deleteLast();
        //list.display();
        
        //list.delete(3);
       // list.deleteHead();
        list.display();
        System.out.println(list.exists(4));
        
        
	}
	
}