Results 1 to 8 of 8
Thread: LinkedList
- 10-12-2009, 08:49 AM #1
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
LinkedList
hey guys
I am doing an assignment regarding the linkedlists where i needed to implement these methods
i pretty much wrked thru every method except the "Undo" method...Method Specification
public void delete(int m, int n);
current is positioned at line m and lines m through n will be removed. After deletion, the line after line n will be the current line.
public void insert(String s);
The parameter string is the new line to be inserted. If there is no current line in the text, the line is attached to the end of the text. Otherwise, the new line will be added before the current line.
public void moveCursorTo(int m);
current is positioned at m.
public void replace(String oldValue, String newValue);
All the occurrences of oldValue is replaced by newValue in the current line.
public void undo();
This method will undo the one step preceding function.
public String display();
It returns a String that represents the text. Suppose there are two lines in the text,
I dont have any idea how to start the method implementation..any heads up would be good..thnx in advance
- 10-12-2009, 09:06 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Your other methods need to push what they've done onto a stack (see the "Stack" class/interface), and undo needs to pop those and "undo" them.
You will need to design a Class, or Classes, for the actions and include one, or more, member variables to maintain links to the affected items.
I.E. an Add class could look like this
Then your "undo" needs simply pop the stack and call the undo method, and your other methods need simply create the "Action" class instances and push them onto the Stack.Java Code:package whatever; public class AddAction { Object item; public AddAction(Object item) { this.item = item; } public void undo() { // remove the node } }
- 10-12-2009, 09:10 AM #3
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
Thnx for the quick reply
ok .. i think i have the idea of using the stacks as i did some online problems rergarding them...but my doubt was my professor still hasnt discussed abt stacks yet..so is there any another way of implementing the undo method..such as double linked list...
- 10-12-2009, 09:58 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
you have D(delete), I(insert), M(move) methods,
you need variable to store the previous method called
you may also need to store the affected index of linkedlist, even value of that index
- 10-12-2009, 05:56 PM #5
It's been a while since I've worked with Stacks and lists so correct me if I'm wrong. By double linked list do you mean circular? Or it's a linked list of doubles or what?
You can implement a stack very easily with a linked list, however if you're using a circular linked list you run into issues since there is no "end" node exactly, popping and inserting become more of a hassle.
What does your undo method do? Undo the last action, delete the last node, what? If it is actually undoing whatever the last action was you're going to have to keep track of what the last action was, simple check using the size of the list, and store the last node deleted until some other action is called.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 10-12-2009, 09:02 PM #6
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
- 10-12-2009, 09:34 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
So? And it's illegal to think for yourself?
In any case, create, then a "lastAction" variable to hold the Action object created in the other methods, and have the "undo" method simply call the "undo" method from this one action object. It means you can only ever undo the last action, and only one action, but hey. The rest is still the way to go.
- 10-13-2009, 12:59 AM #8
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
Similar Threads
-
Help with LinkedList<AnyType>
By wesgarner in forum New To JavaReplies: 2Last Post: 09-24-2009, 03:00 PM -
LinkedList help
By jigglywiggly in forum New To JavaReplies: 6Last Post: 09-19-2009, 07:24 AM -
LinkedList problem
By Mika in forum New To JavaReplies: 7Last Post: 02-18-2009, 02:10 PM -
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:52 AM -
Problem with LinkedList
By Eric in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks