Moving Items in a Circle

COSC 2306 - Queue Data Structures Visualizer


FRONT
(Dequeue)

REAR
(Enqueue)

Controls

# Queue initialized: [1, 2, 3, 4, 5, 6]

Programming Assignment

Write a function CircularShift(mylist, num) that takes a list and a number num. The function should use a Queue to shift the elements of the list num times in a circle.

Task Details:

  • Initialize an empty Queue.
  • Enqueue all elements of mylist into the queue.
  • Dequeue and immediately Enqueue elements num times.
  • Finally, return the result by dequeuing the first item after the shifts are complete.
View Implementation Details

Python implementation of the Circular Shift algorithm using a Queue:

                            
def CircularShift(mylist, num):
    myq = Queue()
    for i in mylist:
        myq.enqueue(i)

    if not myq.isEmpty():
        for i in range(num):
            item = myq.dequeue()
            myq.enqueue(item)
            
    return myq.dequeue()