Results 1 to 8 of 8

Thread: LinkedList

  1. #1
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default LinkedList

    hey guys

    I am doing an assignment regarding the linkedlists where i needed to implement these methods
    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 pretty much wrked thru every method except the "Undo" method...

    I dont have any idea how to start the method implementation..any heads up would be good..thnx in advance

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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
    Java Code:
    package whatever;
    
    public class AddAction {
      Object item;
    
      public AddAction(Object item) {
        this.item = item;
      }
    
      public void undo() {
        // remove the node
      }
    }
    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.

  3. #3
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    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...

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  6. #6
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by xcallmejudasx View Post
    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.
    thnx for the replies..

    My undo jus undo's the last performend action..

    double linked list is a linked list where you can go forward and backward.rather than a single linked list where you can only move forward..and it has a head and a tail to it

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by [RaIdEn] View Post
    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...
    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.

  8. #8
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    oops...sry to forgot to add ...i got the method function ..i used stacks....since the professor started the topic today...thnx for the replies

Similar Threads

  1. Help with LinkedList<AnyType>
    By wesgarner in forum New To Java
    Replies: 2
    Last Post: 09-24-2009, 04:00 PM
  2. LinkedList help
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 09-19-2009, 08:24 AM
  3. LinkedList problem
    By Mika in forum New To Java
    Replies: 7
    Last Post: 02-18-2009, 03:10 PM
  4. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 02:52 AM
  5. Problem with LinkedList
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •