0

Suppose you're given a scenario where you're tasked to code an album editor with the use of LinkedList. Given that you have 2 albums A and B where photos can be inserted, deleted, previewed and undo, how do I go about implementing the undo method?

The input would be: The first line of input will contain a single integer N, the number of operations. It is guaranteed that 0 < N ≤ 500. N lines of input will follow. Each line of input will correspond to one operation.

Among these, it is guaranteed that all albumID parameters will be either “A” or “B”. Also, all position parameters will be an integer between 0 and 1,000,000,000 (1 billion) as well. In addition, all photoID parameters will be an integer between 0 and 1,000,000,000 (1 billion).

e.g. an example of Insert would be:

public void INSERT(Scanner sc) {
  int a = sc.nextInt();
  int b = sc.nextInt();
  if (x.split("\\s+")[1].equals("A"){
    albumA.add(a,b);

Hence, how do I implement UNDO(albumID) with this description:

Description This operation will be provided with a single parameter: albumID and you are supposed to undo the last change to the album identified by albumID. A change to the album is defined as either an insert operation or a delete operation. An undo operation is not considered as a change to the album. In addition, if an insert or delete operation was ignored, it is also not considered as a change to the album.

  • In the case of an insert operation, the corresponding undo action is to remove the inserted photo.
  • In the case of a delete operation, the corresponding undo action is to restore (insert back) the deleted photo (in the same position).
squirrel
  • 309
  • 2
  • 11
  • Please feel free to correct me if my insert method has errors! – squirrel Mar 08 '18 at 07:43
  • 1
    [Be wary of comparing strings using ==.](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Philippe Mar 08 '18 at 07:44
  • 3
    What have you already tried? Why did your attempts fail? – lwi Mar 08 '18 at 07:44
  • I am unsure of how to go about starting it! and thank you for the notice I've changed it to .equals() – squirrel Mar 08 '18 at 07:49
  • i think circular doubly linked list is helpful for this. just updating the tail pointer backwards for undo and moving forward to redo. – Joseph D. Mar 08 '18 at 07:52
  • You can start be creating an interface for `Action` with methods `do()` and `undo()`. You can then have different actions e.g. `InsertAction` that implement the interface. Use a stack to maintain a history of actions – W.K.S Mar 08 '18 at 07:52
  • Also, please follow java naming convention for methods. You should use camelCasing for method names i.e. name your method `insert` instead of `INSERT` – W.K.S Mar 08 '18 at 07:55
  • There are so many questions (and answers) on SO dealing with the undo/redo topic. [Simple search](https://stackoverflow.com/search?q=%5Bjava%5D+undo). Please take the time to study them carefully and adapt the suggestions to your need. As it is, your question is too broad. Nevertheless, I took one of the mentioned questions as a duplicate. – Seelenvirtuose Mar 08 '18 at 07:56

0 Answers0