2017-10-15 6 views
1

연결된 목록을 시계 방향으로 일정량 회전하려고합니다. 링크 된 목록을 시계 방향으로 회전

private class Node { 

    private T data; // Entry in bag 
    private Node next; // link to next node 

    private Node(T dataPortion) { 
     this(dataPortion, null); 
    } // end constructor 

    private Node(T dataPortion, Node nextNode) { 
     data = dataPortion; 
     next = nextNode; 
    } // end constructor 
} // end Node 

public void leftShift(int num){ 

    if (num == 0) return; 

    Node current = firstNode; 

    int count = 1; 
    while (count < num && current != null) 
    { 
     current = current.next; 
     count++; 
    } 

    if (current == null) 
     return; 


    Node kthNode = current; 


    while (current.next != null) 
     current = current.next; 



    current.next = firstNode; 


    firstNode = kthNode.next; 


    kthNode.next = null; 

} 

나는 일을 내 시계 반대 방향으로 회전을 얻을 수 있었다 그러나 나는 내가 이전 노드를 찾을 수 없습니다부터 시계 방향으로 회전을 얻는 방법에 대해 좀 혼란 스러워요.

+0

내가 노드 클래스를 유지하는 것입니다 그리고 난, FIFO의 구현을 ListNode 클래스를 만들 것이다. 그런 다음 queue(), dequeue()를 사용하여 마지막 노드를 가져 와서 큐의 시작 부분에 놓습니다. 예를 쓰고 싶다면 –

+0

예를 들어 도움이 될 것입니다. – FiftySentos

답변

0

예는 질문 :

private class Node { 

    private T data; // Entry in bag 
    private Node next; // link to next node 

    public Node(T dataPortion) { 
     this(dataPortion, null); 
    } // end constructor 

    public Node(T dataPortion, Node nextNode) { 
     data = dataPortion; 
     next = nextNode; 
    } // end constructor 
    T getObject() { 
    return data; 
    } 
    Node<T> getNext() { 
    return next; 
    } 
} // end Node 

public class Queue<T>{ 
  
    private Node head; 
    private Node tail; 
    private String name; 


    public Queue(){ 
     this("queue"); 
    } 
  
    public Queue(String listName) { 
     name = listName; 
     head = tail = null; 
    } 

    public boolean isEmpty() { 
     return tail == null; 
    } 

    public void put(T item) { 
     Node node = new Node(item); 

     if (isEmpty()) // head and tail refer to same object 
      head = tail = node; 
     else { // head refers to new node 
      Node oldtail= tail; 
      tail=node; 
      oldtail.nextNode=tail; 

     } 
    } 

    public Object get() throws NoSuchElementException { 
     if (isEmpty()) // throw exception if List is empty 
      throw new NoSuchElementException(); 
  
     T removedItem = head.data; // retrieve data being removed 
  
     // update references head and tail 
     if (head == tail) 
      head = tail = null; 
     else // locate new last node 
     { 
      head=head.nextNode; 


     } // end else 
  
     return removedItem; // return removed node data 
    } 
    public int size() { 
     int count = 0; 
     if(isEmpty()) return count; 
     else{ 
     Node<T> current = head; 

     // loop while current node does not refer to tail 
     while (current != null){ 
      count++; 
      if(current.nextNode==null)break; 
      current=current.nextNode; 
     } 

     return count; 
    } 
    public void shift(){ 
     if(size()<=1)return; 

     T removed = get(); 
     put(removed); 
    } 
}