#K80162. Recently Used List
Recently Used List
Recently Used List
You are tasked with implementing a data structure called RecentlyUsedList which simulates a list of recently used items. The list should maintain the most recently added or accessed item at the beginning and the oldest at the end. When adding an item:
- If the item does not exist in the list, simply add it to the front.
- If the item already exists, it should be moved to the front, ensuring no duplicates exist in the list.
The data structure should support two operations:
ADD <item>
— Add the given item.GET
— Output the current list from most recent to least recent. The output should be the items separated by a space on a single line.
</p>
ADD <item>
— where<item>
is a string, orGET
— which requires the program to print out the current list.
Note: All input is read from standard input (stdin) and output is printed to standard output (stdout). If multiple GET
commands appear, print the result for each in the order they appear.
For example, executing the following sequence of operations:
\[ \texttt{ADD item1}\newline \texttt{ADD item2}\newline \texttt{ADD item3}\newline \texttt{ADD item2}\newline \texttt{GET} \]should produce the output:
\[ \texttt{item2 item3 item1} \]inputFormat
The first line of input contains an integer n, the number of operations. Each of the next n lines contains a command. A command is either:
outputFormat
Whenever a GET
command is encountered, output a single line containing the items in the list separated by a single space, from the most recent to the least recent item.
5
ADD item1
ADD item2
ADD item3
ADD item2
GET
item2 item3 item1
</p>