0

I know there is a lot of information out there on a undo/redo text type editor and I have read through quite them but still lack understanding of how to apply it to my code.

I'm using a linked list to hold a first part of the sentence and every word after the first part is entered can be added, removed, undone or redone to the linked list.

Pushing the command and word during an add I understand, manipulation of the undo and redo I understand.

When I perform an add or delete command, the redo stack is to be empty.

What I don't understand is how to push the command and data to be deleted from the list onto the undo stack.

The snippet of code I'm working with is as follows:

    case "delete"://removes linked list node with the string specified on undo stack.
        if(!undo.isEmpty()){    
        undo.topAndPop();//removes the previous command/choice off the undo stack.
        temp = undo.top();//reads the data on the next stack segment after the command is gone.
        readme.listRemove(temp);//removes the data from the linked list.
        undo.pop();//removes the tos data
        redo.makeEmpty();//empties redo stack.
        }else break;
        break;
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • 1
    Tell us the exact problem you are facing. You dunno how to call `Stack.push()`? You dunno how to model a "command"? You want us to verify your design? Get a Design Pattern book and check for the Memento and Command pattern, usually it provides an example on Undo-Redo. – Adrian Shum Jul 13 '17 at 02:27
  • I think my largest problem is how to model a command. I know there is a much simpler way to achieve what I'm trying to, I just don't know about it. Should another class be implemented to allow for the commands? What would be some starting points? – charonodaemon Jul 13 '17 at 02:36
  • Do you want to use a single stack for command and data or apart? – Vasyl Lyashkevych Jul 13 '17 at 02:44
  • I can guess you should use two stacks: one to Undo operations and the second one for Redo operations. – Vasyl Lyashkevych Jul 13 '17 at 02:50
  • I have those two ListStack types implemented for the undo and redo operations. The problem lies in when I delete a node from my linked list holding the strings/data whatnot, I have to push the data and the command somewhere. I have data and the add commands pushed onto the undo stack when I use the add function and if I delete a node and push the information back on to the undo stack, I get back what I just deleted from the node and stack. Dilemma for sure. – charonodaemon Jul 13 '17 at 02:54
  • It is probably too broad for a question to be answered in SO. Get a design pattern book and check for the patterns I mentioned, there should be some concrete example (I do remember the GoF C++ Design Pattern does provide example of editor undo/redo) – Adrian Shum Jul 13 '17 at 03:06
  • I appreciated the feedback. Tired and getting tunnel vision. – charonodaemon Jul 13 '17 at 03:15
  • Alright! Think I have it figured. I was thinking about a way to store the command and the attribute to be done and undone. Being a class assignment, it wasn't specified as to how many stacks could be used after clarification. Created two more stacks to hold the text/data being added/removed from the linked list and push/popping commands from the undo and redo stacks. Thank you all for your input! – charonodaemon Jul 14 '17 at 00:51

0 Answers0