JAVA : Need OUTPUT Move-to-front. Read in a sequence of characters from standard
ID: 3793417 • Letter: J
Question
JAVA : Need OUTPUT
Move-to-front. Read in a sequence of characters from standard input and maintain the characters in a linked list with no duplicates. When you read in a previously unseen character, insert it at the front of the list. When you read in a duplicate character, delete it from the list and reinsert it at the beginning. Name your program MoveToFront: it implements the well-known move-to-front strategy, which is useful for caching, data compression, and many other applications where items that have been recently accessed are more likely to be reaccessed.Explanation / Answer
Output:
Enter The Sequence Of Characters In String
nitin
List Is :[n, i, t]
Do U Want To Enter More(y/n): y
Enter The Sequence Of Characters In String
bhasin
List Is :[n, i, s, a, h, b, t]
Do U Want To Enter More(y/n): n
Program:
import java.util.ArrayList;
import java.util.Scanner;
public class MoveToFront{
static ArrayList<Character> array;
public static void main(String[] args) {
array = new ArrayList<>();
boolean isMore = false;
Scanner s = new Scanner(System.in);
do {
System.out.println("Enter The Sequence Of Characters In String");
String in = s.next();
for (int i = 0; i < in.length(); i++) {
char str=in.charAt(i);
if (array.contains(str)) {
array.remove(array.indexOf(str));
}
array.add(0, str);
}
System.out.println("List Is :"+ array);
System.out.print("Do U Want To Enter More(y/n): ");
if(s.next().equals("y"))
isMore=true;
else
isMore=false;
} while (isMore);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.