Data Structures, Algorithms, & Applications in Java
Chapter 6, Exercise 19

(a)
A circular shift by i, 0 < i < size can be done by moving the first i elements from the front of the chain to the end. The code is given below.

public class ExtendedChainWithCircularShift extends ExtendedChain
{
   /** shift the elements clockwise circularly by shiftAmount
     * throws IllegalArgumentException when
     * shiftAmount < 0 */
   private void circularShift(int shiftAmount)
   {
      // empty list is easily shifted by any amount
      if (size == 0)
         return;

      // validate shiftAmount
      if (shiftAmount < 0)
         throw new IllegalArgumentException
                   ("shift amount must be >= 0, it is " + shiftAmount);

      // get equivalent amount in the range 0 though size - 1
      shiftAmount %= size;
  
      // expedite zero shift, makes rest of code easier
      if (shiftAmount == 0)
         return;

      // find last node to be moved to right end
      ChainNode lastNodeToMove = firstNode;
      for (int i = 1; i < shiftAmount; i++)
         lastNodeToMove = lastNodeToMove.next;

      // move nodes from front to rear
      lastNode.next = firstNode;
      firstNode = lastNodeToMove.next;
      lastNode = lastNodeToMove;
      lastNode.next = null;
   }
}





(b)
The test program and output are in the files ExtendedChainWithCircularShift.*.