Answered step by step
Verified Expert Solution
Question
1 Approved Answer
An important application of the Stack data structure is implementing web browser Undo and Redo operations for a text editor. Write a program in Java
An important application of the Stack data structure is implementing web browser
Undo and Redo operations for a text editor. Write a program in Java to implement
the same. The user will type in a sentence. Store it in a String variable. Now extract
each word from the string from left to right and insert them in a linked list, where
each word will be stored in separate nodes. Now, show a menu to the user to modify a word, delete a word, undo the operations, and redo the operations. Use stack data structure to store the modified deleted word and its position. The Undo operation will insert the word to its original position, and the Redo operation will repeat the previous modify or delete operation. You can use the Java builtin LinkedList and Stack classes.
The sample inputs outputs are given below:
Enter the sentence: The quick brown fox jumps over the lazy dog
Press to delete a word
Press to change a word
Press to undo an operation
Press to redo an operation
Press to exit
Enter your choice:
Sorry, cant undo any previous operations
Enter your choice:
Sorry, cant redo any previous operations
Enter your choice:
Enter the word to modify: brown
Enter the new word: red
The word is changed. The modified sentence is:
The quick red fox jumps over the lazy dog
Enter your choice:
Enter the word to delete: over
The word is removed. The modified sentence is:
The quick red fox jumps the lazy dog
Enter your choice:
Undo operation is done. The sentence: The quick red fox jumps over the lazy dog
Enter your choice:
Undo operation is done. The sentence: The quick brown fox jumps over the lazy dog
Enter your choice:
Redo operation is done. The sentence: The quick red fox jumps over the lazy dog
Enter your choice:
Thank you!
Hint: Use two stacks, one for undo operation and another for redo operation. Both
stacks will contain the changed deleted word, its position, and the operation modify or delete In case of a modify or delete operation, push the original word and its position into the undo stack. In case of an undo operation, pop the topmost word and its position, insert the word in the proper position of the sentence and finally, push them in the redo stack. In case of a redo operation, pop the topmost word, modify delete the word from the sentence as per the operation, and push the information into the undo stack. If there are multiple occurrences of the same word, change or delete the first occurrence.
The linked list representation of the sentence The quick brown fox:
The sample undo stack:
In the above sample undo stack, each cell contains three information, the word, its
position in the sentence, and the operation d delete, m modify Create the redo
stack similarly.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started