Double Linked Structure
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Double Linked Structure

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Double Linked Structure

    Hello, this code right here needs to take advantage of a doubly linked structure, currently its set up to
    a single one, it needs to be without the previous variable and I suspect it should somehow use this .

    for exmaple the add method taking advantage I believe looks somewhat like this (although I haven't tested it)

    public void add (T element)
    {

    if (!(contains(element)))
    {
    DoubleNode<T> node = new DoubleNode<T> (element);
    node.setNext(contents);
    contents = node;
    contents.setPrevious(contents.getPrevious());
    count++;
    }
    }


    Now the remove method canot have the previous declaration, im assuming it needs to work with a line like the add
    does like this " contents.setPrevious(contents.getPrevious());" Thanks.

    public T remove (T target) throws EmptySetException,
    NoSuchElementException
    {
    boolean found = false;
    DoubleNode<T> previous, current;
    T result = null;

    if (isEmpty())
    throw new EmptySetException();

    if (contents.getElement().equals(target))
    {
    result = contents.getElement();
    contents = contents.getNext();
    }
    else
    {
    previous = contents;
    current = contents.getNext();
    for (int look=0; look < count && !found; look++)
    if (current.getElement().equals(target))
    found = true;
    else
    {
    previous = current;
    current = current.getNext();
    }

    if (!found)
    throw new NoSuchElementException();

    result = current.getElement();
    previous.setNext(current.getNext());
    }

    count--;

    return result;
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Double Linked Structure

    Please use code tags when posting.
    this code right here needs to take advantage of a doubly linked structure
    Where is this double linked structure you are supposed to be using and why doesn't the doubly linked structure handle adds, removes etc?
    it needs to be without the previous variable
    That makes no sense to me and you explain what you are trying to do.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2012
    Posts
    2

    Re: Double Linked Structure

    Well this is just a portion of a code, the idea is that the remove method instead of single linked now handles a double link, therefore it no longer needs a previous variable and needs to be able to jump straight from the contents variable to previous and next.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Double Linked Structure

    I know this is just a portion of the code but what code is it a portion of? It makes little sense on its own without a full explanation.

    I still don't understand what you mean by no longer needing a previous variable. Singly linked structures have a single reference to another node, generally it is a reference to the next node in the structure (although there's no reason you couldn't do one with a previous reference instead), doubly linked structures have both a next and a previous reference hence the name doubly linked. How can you have a doubly linked structure without a previous reference?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Double Linked Structure

    @OP: Please use the [code][/code] tags to preserve indentation (it will not format unformatted code, though).
    Otherwise, people will have a hard time reading your code and understanding your question!

    @keang: He's referring to this declaration near the top of the remove() method - the local helper variable "previous":
    DoubleNode<T> previous, current;

    Basically, the code searches for the node that matches the remove target, simultaneously keeping track of the node it previously tested, so that, later on, it can do this:
    previous.setNext(current.getNext());

    @OP: Well, If you have a doubly linked list, you can simplify the code, because then the node class would have both getNext() and getPrevious(). Once you've found the target, you can simply do something like this:


    DoubleNode<T> previous = current.getPrevious();
    previous.setNext(current.getNext());
    Or:

    current.getPrevious().setNext(current.getNext());


    The way you wanted to do it wouldn't work:
    contents.setPrevious(contents.getPrevious());
    This doesn't change anything. It's saying "set my previous to my current previous"...
    Last edited by TheGreatCthulhu; October 13th, 2012 at 07:11 PM.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Double Linked Structure

    Quote Originally Posted by TheGreatCthulhu
    @keang: He's referring to this declaration near the top of the remove() method - the local helper variable "previous":
    DoubleNode<T> previous, current;

    Basically, the code searches for the node that matches the remove target, simultaneously keeping track of the node it previously tested, so that, later on, it can do this:
    previous.setNext(current.getNext());
    Thanks for pointing that out.
    If the OP can't be bothered to use code tags after being asked to do so then I generally don't bother reading their unformatted code.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center