메뉴 바로가기 검색 및 카테고리 바로가기

한빛미디어

뇌를 자극하는 알고리즘

뇌를 자극하는 알고리즘 질의응답 게시판입니다.

헐~ 자바코드 포함해서 다시 올립니다. ㅎㅎ

2010-01-21

|

by jhw863

2510

class Node{
        private int data;
           private Node beforenode = null;
        private Node nextnode = null;
        
        protected int getData() {
                return data;
        }

        protected void setData(int data) {
                this.data = data;
        }

        protected Node getBeforenode() {
                return beforenode;
        }

        protected void setBeforenode(Node beforenode) {
                this.beforenode = beforenode;
        }

        protected Node getNextnode() {
                return nextnode;
        }

        protected void setNextnode(Node nextnode) {
                this.nextnode = nextnode;
        }

        public Node(){
                beforenode = null;
                nextnode = null;
        }
        
}

class DList{
        public Node createNode(int data){
                Node newnode = new Node();
                
                newnode.setData(data);
                
                return newnode;
        }
        
        public void addNode(Node head, Node newnode){
                
                if(head == null){
                        head = newnode;
                }
                
                else{
                        Node tail = head;
                        
                        while(tail.getNextnode() != null){
                                tail = tail.getNextnode();
                        }
                        
                        tail.setNextnode(newnode);
                        newnode.setBeforenode(tail);
                }
        }
        
        public void insertAfter(Node current, Node newnode){
                newnode.setNextnode(current.getNextnode());
                newnode.setBeforenode(current);
        
                if(current.getNextnode() != null){
                        current.getNextnode().setBeforenode(newnode);
                        current.setNextnode(newnode);
                }
        }
        
        public void removeNode(Node head, Node remove){
                if(head == remove){
                        head = remove.getNextnode();
                        
                        if(head != null)
                                head.setBeforenode(null);
                        
                        remove.setBeforenode(null);
                        remove.setNextnode(null);
                }
                
                else{
                        Node temp = remove;
                        
                        if(remove.getBeforenode() != null)
                                remove.getBeforenode().setNextnode(temp.getNextnode());
                        
                        if(remove.getNextnode() != null)
                                remove.getNextnode().setBeforenode(temp.getBeforenode());
                        
                        remove.setBeforenode(null);
                        remove.setNextnode(null);
                }
        }
        
        public Node getNodeAt(Node head, int location){

                Node current = head;
                
                while(current != null && (--location) >= 0){
                        current = current.getNextnode();
                }
                
                return current;
        }
        
        public int getCountNode(Node head){
                int count = 0;
                Node current = head;
                
                while(current != null){
                        current = current.getNextnode();
                        count++;
                }
                return count;
        }
        
        public void printNode(Node node){
                if(node.getBeforenode() == null)
                        System.out.println("Prev: NULL");
                else
                        System.out.println("Prev: "+node.getData());
                
                System.out.println("Current: "+node.getData());
                
                if(node.getNextnode() == null)
                        System.out.println("Next: NULL");
                else
                        System.out.println("Next: "+node.getNextnode().getData());
        }
}
public class Test {
        
        public static void main(String[] args) {
        
                int count=0;
                Node list = new Node();
                Node newnode = new Node();
                Node current = new Node();
                
                DList node = new DList();
                
                for(int i=0; i<5; i++){
                        newnode = node.createNode(i);
                        node.addNode(list, newnode);
                }
                
                count = node.getCountNode(list);
                for(int i=0; i                         current = node.getNodeAt(list, i);
                        System.out.println("List["+i+"] : "+current.getData());
                }
                System.out.println("\nInserting 3000 After [2]...\n");
        }
}
댓글 입력