- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to design a queue that moves the most recently used element to the end of it in Python

Suppose, we are asked to design a queue that moves the most recently used element to the end of it. The queue will be initialized with integer numbers 1 to n. Now we have to build a function so that whenever it is called, it moves the value from the position given as its input to the end of the queue. We will call the function multiple times, and the function will return the value currently at the end of the queue while performing its moving task.

So, if the queue is initialized with a value n = 5, or it contains values from 1 to 5.; and the positions where the move is performed are 5, 2, 3, and 1 respectively, then the output will be 5, 2, 4, 1

To solve this, we will follow these steps −

- i := the position in array index - 1, where the value k can be inserted to the right of maintaining the sorted order
- x := delete (k - index[i])th element from data[i]
- for ii in range i+1 to size of index, do
- index[ii] := index[ii] - 1

- if size of last element of data>= nn, then
- insert a new list at the end of list data
- insert n at the end of list index

- insert x at the end of data
- if data[i] is rmpty, then
- delete ith element from data
- delete ith element from index

- return x

## Example

Let us see the following implementation to get better understanding −

from bisect import bisect_right from math import sqrt class TestQueue: def __init__(self, n): self.n = n self.nn = int(sqrt(n)) self.data = [] self.index = [] for i in range(1, n+1): ii = (i-1)//self.nn if ii == len(self.data): self.data.append([]) self.index.append(i) self.data[-1].append(i) def solve(self, k): i = bisect_right(self.index, k)-1 x = self.data[i].pop(k - self.index[i]) for ii in range(i+1, len(self.index)): self.index[ii] -= 1 if len(self.data[-1]) >= self.nn: self.data.append([]) self.index.append(self.n) self.data[-1].append(x) if not self.data[i]: self.data.pop(i) self.index.pop(i) return x queue = TestQueue(5) print(queue.solve(5)) print(queue.solve(2)) print(queue.solve(3)) print(queue.solve(1))

## Input

queue = TestQueue(5) print(queue.solve(5)) print(queue.solve(2)) print(queue.solve(3)) print(queue.solve(1))

## Output

5 2 4 1

- Related Articles
- Program for K Most Recently Used (MRU) Apps in C++
- Program to find frequency of the most frequent element in Python
- Write a program in Python to find the most repeated element in a series
- Program to find out the index of the most frequent element in a concealed array in Python
- Write a program in Python to print the most frequently repeated element in a series
- C# program to find the most frequent element
- Program to Implement Queue in Python
- Program to find number m such that it has n number of 0s at end in Python
- Add an object to the end of the Queue - Enqueue Operation in C#
- Write an algorithm that takes an array and moves all of the zeros to the end JavaScript
- Python Program to move numbers to the end of the string
- Program to implement a queue that can push or pop from the front, middle, and back in Python
- Program to recover shuffled queue of people in python
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- Design a DFA that accepts at most 3 a"s