ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • LinkedList (2)
    과거...../알고리즘 & 자료구조 2011. 4. 18. 15:27
    이중 연결 리스트의 구조는 단순 연결 리스트와 비슷하지만, 포인터 공간이 두 개가 있고 각각의 포인터는 앞의 노드와 뒤의 노드를 가리킨다


    public class CLinkedList {
    	
    	private ListNode head = null;
    	
    	public CLinkedList(){
    		this.head = null;
    	}
    
    	public void insertFirstNode(String data){
    		ListNode newNode = new ListNode(data);
    		if(this.head == null){
    			this.head = newNode;
    			this.head.link = head;
    		}else{
    			ListNode temp = this.head;
    			while(temp.link != head){
    				temp = temp.link;
    			}
    			
    			newNode.link = temp.link;
    			this.head = newNode;
    			temp.link = newNode;
    		}
    		
    	}
    	
    	
    	public void insertMiddleNode(ListNode pre, String data){
    		ListNode newNode = new ListNode(data);
    		newNode.link = pre.link;
    		pre.link = newNode;
    	}
    	
    	
    	public void insertLastNode(String data){
    		ListNode newNode = new ListNode(data);
    		if(head == null){
    			this.head = newNode;
    			this.head.link = head;
    		}else{
    			ListNode temp = head;
    			while(temp.link != head) temp = temp.link;
    			temp.link = newNode;
    			newNode.link = head;
    		}
    	}
    	
    	public void deleteLastNode(){
    		ListNode pre, temp;
    		if(head == null) return ;
    		if(head.link == null){
    			head = null;
    		}else{
    			pre = head;
    			temp = head.link;
    			while(temp.link != head){
    				pre =temp;
    				temp = temp.link;
    			}
    			pre.link = head;
    		}
    	}
    	
    	public ListNode searchNode(String data){
    		ListNode temp = this.head;
    		while(temp.link != head && temp != null){
    			if(data == temp.getData()) return temp;
    			else temp = temp.link;
    		}
    		return temp;
    	}
    	
    	public void printList(){
    		ListNode temp = this.head;
    		System.out.printf("L =(");
    		while(temp != null && temp.link != this.head){
    			System.out.printf(temp.getData());
    			temp = temp.link;
    			if(temp != null && temp.link != this.head){
    				System.out.printf(", ");
    			}
    		}
    		System.out.println(")");
    	}
    	
    	
    }
    
    public class CLinkedListMain {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		CLinkedList L = new CLinkedList();
    		System.out.println("add linkedList");
    		L.insertLastNode("1");
    		L.insertLastNode("2");
    		L.insertLastNode("3");
    		L.printList();
    		
    		System.out.println("add node(first)");
    		
    		L.insertFirstNode("first");
    		L.printList();
    		
    		System.out.println("add node after 1");
    		ListNode pre = L.searchNode("1");
    		L.insertMiddleNode(pre, "ex");
    		L.printList();
    		
    		System.out.println("delete");
    		L.deleteLastNode();
    		L.printList();
    		
    
    	}
    
    }
    

    참고 자료 및 소스 출처:

    1. 위키
    2. http://www.gurubee.net/pages/viewpage.action?pageId=1507898

Designed by Tistory.