12,641 views
35 votes
35 votes
Write a new queue function called move_to_rear that moves the element currently at the front of the queue to the rear of the queue. The element that was second in line will be the new front element. Do these using functions push, front, and pop.

User Askaga
by
2.7k points

1 Answer

11 votes
11 votes

Answer:

To implement a move_to_rear function in Python, you can use the push, front, and pop functions to add and remove elements from the queue. Here is an example of how you could implement the move_to_rear function:

def move_to_rear(queue):

# Use the front() function to get the element at the front of the queue

front_element = queue.front()

# Use the pop() function to remove the element from the front of the queue

queue.pop()

# Use the push() function to add the element to the rear of the queue

queue.push(front_element)

This function takes a queue as an input, and uses the front, pop, and push functions to move the element at the front of the queue to the rear of the queue.

Here is an example of how you could use the move_to_rear function:

# Create a queue

queue = Queue()

# Add some elements to the queue

queue.push(1)

queue.push(2)

queue.push(3)

# Move the element at the front of the queue to the rear of the queue

move_to_rear(queue)

# Print the queue to verify that the element has been moved

print(queue) # Output: Queue(2, 3, 1)

In this example, the queue initially contains the elements 1, 2, and 3. The move_to_rear function is called with the queue as the input, which moves the element at the front of the queue (the element 1) to the rear of the queue. After the move_to_rear function is called, the queue contains the elements 2, 3, and 1, in that order.

User IUnknown
by
2.9k points