java-单链表

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));
        
        
	}
	
}

猜你喜欢

转载自chengjianxiaoxue.iteye.com/blog/2110688