728x90
300x250

[자료구조(Data Structure)] - 6. Linked Queue

 

1. Linked Queue 동작 로직

 

a. 삽입(push)

 

b. 인출(pop)

 

 

2. Linked Queue ADT

 

 abstract interface Queue{

       abstract void Push( Object x );

       abstract Object pop();

       abstract boolean isEmpty();

 }

 

 Queue Interface ADT

 

 

 
class LinkedNode{
        private Object data;
        private LinkedNode next;
 
        public void setData(Object x)
        {
             this.data = x;
        }


       public void setNext(LinkedNode node)
       {
             this.next = node;
       }

       public Object getData()
       {
            return this.data;
       }

       public LinkedNode getNext()
       {
            return this.next;
       }
}

LinkedNode ADT

 

3. 코드 구현

 

 

 abstract interface Queue{

       abstract void Push( Object x );

       abstract Object pop();

       abstract boolean isEmpty();

 }

 class LinkedNode{
        private Object data;
        private LinkedNode next;
 
        public void setData(Object x)
        {
             this.data = x;
        }


       public void setNext(LinkedNode node)
       {
             this.next = node;
       }

       public Object getData()
       {
            return this.data;
       }

       public LinkedNode getNext()
       {
            return this.next;
       }
}

 class LinkedQueue implements Queue{

          private LinkedNode front;
          private LinkedNode rear;
 
          public LinkedQueue(){
                 this.front = null;
                 this.rear = null;
          }
 
          public void push(Object x){
  
                 LinkedNode newNode = new LinkedNode();

                 newNode.setData( x );
                 newNode.setNext( null );

              

                 if( isEmpty() )
                 {
                       this.front = newNode;
                       this.rear = newNode;  
                 }
                 else{
                       this.rear.setNext ( newNode );
                       this.rear = newNode;
                 }

          }

 

          public Object pop(){
   
                 if ( isEmpty() )
                       return null;
  
                 Object data = front.getData();
   
                 LinkedNode prevnode = front;
                 front = front.getNext();
 
                 prevnode = null;
                 return data;
         }

 

         public Object peak(){
  
                if ( isEmpty() )
                        return null;
                else
                        return front;

         }

 

         public boolean isEmpty(){
  
                if(front == null)
                        return true;
                else
                        return false;  

          }
 
          public void viewOfAll()
          {
                LinkedNode current = this.front;
  
                System.out.println("출력 ---------");
 
                while ( current != null )
                {
                         System.out.println( current.getData() );
                         current = current.getNext();
                 }

           }

 }

 
public class LinkedQueueProgram{
 
         public static void main(String args[])
         {
                 LinkedQueue queue = new LinkedQueue();
                 queue.push("1번");
                 queue.push("2번");
                 queue.push("3번");
                 queue.viewOfAll();

 

                 queue.pop(); 
                 queue.viewOfAll();
 
                 queue.push("1번");
                 queue.viewOfAll();

         }
}

 LinkedQueueProgram.java

 

 

반응형

+ Recent posts