링크 리스트 의 간단 한 실현
                                            
 3271 단어  데이터 구조 와 알고리즘
                    
package mywork.c20150605;
import mywork.c20150605.LinkList.Node;
/*
 * a simple demo for LinkList
 * HanChun 2015.6.5
 */
public class LinkListDemo {
	class Node{
		T data;
		Node next;
		
		public Node(){
			
		}
		
		public Node(T data, Node next){
			this.data = data;
			this.next = next;
		}
	}
	
	Node header;
	Node tail;
	int size;								//        
	
	public LinkListDemo(){
		header = null;
		tail = null;
	}
	
	public LinkListDemo(Node element){
		header = element;
		tail = header;
		size++;
	}
	
	//       
	public int getSize(){
		return size;
	}
	
	//         
	public boolean isEmpty(){
		if(size == 0){
			return true;
		}
		return false;
	}
	
	//       
	public Node getNodeByIndex(int index){
		if(index < 0 || index > size){
			throw new IndexOutOfBoundsException("    ");
		}
		Node current = header;
		for(int i = 0; i < size && current != null; current = current.next, i++){
			if(i == index){
				return current;
			}
		}
		return null;
	}
	
	//             
	public int locate(T element){
		Node current = header;
		for(int i = 0; i < size && current != null; current = current.next, i++ ){
			if(current.data.equals(element)){
				return i;
			}
		}
		return -1;
	}
	
	/*
	 *               ,   :        
	 *            ,        ,      
	 *      。
	 */
	public void addAtHeader(T element){
		header = new Node(element, header);
		if(tail == null){
			tail = header;
		}
		size++;
	}
	
	//       
	public void add(T element){
		if(header == null){
			header = new Node(element, null);
			tail = header;
		}else{
			Node newNode = new Node(element, null);
			tail.next = newNode;
			tail = newNode;
		}
		size++;
	}
	
	//         
	public void insert(T element, int index){
		if(index < 0 || index > size){
			throw new IndexOutOfBoundsException("    ");
		}
		if(header == null){
			add(element);
		}else{
			if(index == 0){
				addAtHeader(element);
			}else{
				Node prev = getNodeByIndex(index - 1);
				prev.next = new Node(element, prev.next);
			}
		}
		size++;
	}
	
	/*
	 *         ,       ,           ,   
	 *        NuLLPointException
	 */
	public void delete(int index){
		if(index < 0 || index > size){
			throw new IndexOutOfBoundsException("    ");
		}
		if(index == 0){
			header = header.next;
		}else{
			Node prev = getNodeByIndex(index - 1);
			Node del = prev.next;
			prev.next = del.next;
			del = null;
		}
		size--;
	}
	
	public String toString(){
		if(isEmpty()){
			return "[]";
		}
		StringBuilder sb = new StringBuilder("[");
		for(Node current = header; current != null ; current = current.next){
			sb.append(current.data.toString() + ",");
		}
		int len = sb.length();
		return sb.delete(len-1, len).append("]").toString();
	}
}
 이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[JAVA] 배열 회전 출력요소 가 출력 을 시작 하 는 위치 에 주의 하 십시오. 모두 몇 라운드 의 수출 이 있 습 니까? n/2 + 1 매 라 운 드 는 상, 우, 하, 좌 로 나 뉜 다. 각 방향의 시작 위치 와 좌표 의 관 계 를 구...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.