728x90
300x250

[자료구조(Data Structure)] - 4. Circular Linked List - Java

 

작년에 배운 자료구조 복습 겸 다시 짜봤습니다.

LinkedList Class 내에 headnode와 firstnode를 보통은 두고 짜는 데 한번 rootnode 하나만 두고 대신 length(정수형)을 추가하여 구현해봤습니다.

 

전체 로직은 이렇습니다.

 

 

/*      
       Data Structure Circular Linked List - rootnode만 이용
       2015. 4. 25
       James
*/ 

 

class Node

{
        private Object data;
        private Node prev;
        private Node next;

 

        // Getter
        public Object getdata()
        {
             return this.data;
        }

        public Node getprev()
        {
             return this.prev;
        }

        public Node getnext()
        { 
             return this.next;
        }
 

        // Setter
        public void setdata(Object data)
       {
             this.data = data;
       }

       public void setprev(Node prev)
       {
             this.prev = prev;
       }

       public void setnext(Node next)
       {
             this.next = next;
       }


class LinkedList{

       private Node headnode;
       private long length;
 
       public LinkedList(){
              this.headnode = null;
              this.length = 0;
       }

       public void insert(Object data)
       {
              Node createnode = new Node();  

              createnode.setdata(data);
              createnode.setprev( createnode );
              createnode.setnext( createnode );
  
              if ( this.headnode == null )
              {
                       this.headnode = createnode;
              }
              else
              { 
                      Node firstnode = this.headnode.getnext();   
   
                      // 현재 기억하고 있는 이전 노드 설정
                      createnode.setprev ( this.headnode );

                      // 맨 마지막 노드 처음 위치 지정
                      createnode.setnext ( firstnode );

                      // 현재 노드 -> node 삽입 (Next)
                      this.headnode.setnext( createnode );
   
                      // 현재 노드 -> 다음 노드로 이동
                      this.headnode = this.headnode.getnext();

                      // 맨 처음 노드 마지막 위치 지정
                      firstnode.setprev( this.headnode );
              }

              this.length++;
  
 }
 
 public void remove(Object data)
 {
             Node currentnode = this.headnode.getnext();
             boolean check = false;  

             long cnt = 0;  
  
             // 데이터 검색
             while ( cnt < length )
             {
                       if( currentnode.getdata() == data)
                       {    
                               check = true;
                               break;
                       }

                       currentnode = currentnode.getnext();
   
                       cnt++;
             }

 

              // 3가지 기준 (맨 처음 자료, 중간 자료, 맨 마지막 자료)
              if ( check == true )
              {
                      Node firstnode = this.headnode.getnext();
                      Node lastnode = this.headnode;
  
                      // 맨 처음 자료
                      if ( currentnode == firstnode )
                      { 
                                // 다음 주소가 firstnode와 일치시
                                if ( this.headnode.getnext ( ) == firstnode ){

                                        Node prevnode = firstnode.getprev();
                                        Node nextnode = firstnode.getnext();
     
                                        firstnode = nextnode;
                                        firstnode.setprev ( prevnode ) ;
                                        this.headnode.setnext ( firstnode ) ;
                               }
                               else
                                        this.headnode = this.headnode.getnext ( ) ; 
                      }

 

                     // 중간 자료 판별
                     if ( currentnode != firstnode && currentnode != lastnode )
                     {
                               Node prevnode = currentnode.getprev ( ) ;
                               Node nextnode = currentnode.getnext ( ) ;

                               currentnode = prevnode ;
                               currentnode.setnext ( nextnode ) ;
                               nextnode.setprev ( prevnode ); 
                     }

 

                      // 마지막 자료 판별
                      if ( currentnode == lastnode && this.headnode != null )
                      {
                               Node prevnode = currentnode.getprev ( ) ;

                        
                               prevnode.setnext ( firstnode ) ;
                               firstnode.setprev ( prevnode ) ;
     
                               currentnode = null ;

                               this.headnode = prevnode;
                      }
 
                      this.length--;

                }  

        }
  
        public void view()
        {
                  Node currentnode = this.headnode.getnext();
    
                  long cnt = 1;

                  while ( cnt <= length )
                  {
                           System.out.println( "번호:" + cnt + ", Data:" + currentnode.getdata() ) ;
   
                           currentnode = currentnode.getprev() ;
                           cnt++;
                  } 
         }
 
         public long getlength()
         {
                  return this.length;
         }

 

          public Node getnode()
          {
                  return this.headnode;
          }

}

 

public class Ds{

         public static void main(String args[])
        {
             LinkedList list = new LinkedList();

             list.insert("야호1"); 
             list.view();

             list.remove("야호1");
             System.out.println( list.getlength() ) ;
             list.view();
       }

 Ds.java

 

반응형

+ Recent posts