Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: trouble with add and remove method on doubly linked list

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default trouble with add and remove method on doubly linked list

    Hello,

    I'm new to this forum so I apologize for any mistakes I'm going to make, I'm still learning the ropes. I am writing a token ring simulation program and am using a circular doubly linked list in the process. I seem to be having issues with the add and remove methods however.

    My TokenRingList class:
    [highlight = java]

    import support.DLLNode;

    /**
    * A specialized circular doubly linked list for a token ring simulation.
    */
    public class TokenRingList implements TokenRingListInterface {
    // The start of the token ring list:
    private DLLNode<Workstation> list;
    // The location used to search this list:
    private DLLNode<Workstation> location;
    // The size of this list:
    private int size;
    // clockwise/counterclockwise - true if clockwise, false if counterclockwise:
    private boolean clockwise = true;

    /**
    * Creates a new TokenRingList object.
    */
    public TokenRingList() {
    list = null;
    location = null;
    }

    /**
    * Returns the size of the token ring.
    */
    public int size() {
    return size;
    }

    /**
    * Adds a Workstation object to this token ring.
    */
    public void add(Workstation element) {
    DLLNode<Workstation> newNode = new DLLNode<Workstation>(element);
    if(list==null)
    {
    list = newNode;
    newNode.setForward(list);
    }
    else
    {
    newNode.setBack(location.getBack());
    newNode.setForward(location);
    location.getBack().setForward(newNode);
    location.setBack(newNode);
    }
    }

    /**
    * Removes a Workstation object from this token ring.
    */
    public boolean remove(Workstation element) {
    DLLNode<Workstation> newNode = new DLLNode<Workstation>(element);
    boolean test = false;
    if (contains(element))
    {
    newNode.getBack().setForward(newNode.getForward()) ;
    newNode.getForward().setBack(newNode.getBack());
    test = true;
    }
    else
    test = false;
    size--;
    return test;
    }


    /**
    * Returns true if this token ring has the workstation; false otherwise.
    */
    public boolean contains(Workstation element) {
    // Is the list empty?
    if (list == null)
    return false;

    // Find the element if it exists:
    DLLNode<Workstation> t = list;
    do {
    if (element == t.getInfo())
    return true;
    t = t.getForward();
    } while (t != list);

    // Not found:
    return false;
    }

    /**
    * Returns the workstation if it is in this token ring; null otherwise.
    */
    public Workstation get(Workstation element) {
    // Is the list empty?
    if (list == null)
    return null;

    // Find the element if it exists:
    DLLNode<Workstation> t = list;
    do {
    if (element == t.getInfo())
    return element;
    t = t.getForward();
    } while (t != list);

    // Not found:
    return null;
    }

    public void reset() {
    location = list;
    }

    /**
    * Returns the next workstation in the token ring.
    */
    public Workstation getNext() {
    if (clockwise) {
    return getClockwise();
    }
    else {
    return getCounterClockwise();
    }
    }

    /**
    * Returns the current workstation and advances location clockwise.
    */
    private Workstation getClockwise() {
    DLLNode<Workstation> t = location;
    location = location.getForward();
    return t.getInfo();
    }

    /**
    * Returns the current workstation and advances location counterclockwise.
    */
    private Workstation getCounterClockwise() {
    DLLNode<Workstation> t = location;
    location = location.getBack();
    return t.getInfo();
    }

    /**
    * Sets the token ring to retrieve workstations in a clockwise fashion.
    */
    public void setClockwise() {
    clockwise = true;
    }

    /**
    * Sets the token ring to retrieve workstations in a counterclockwise fashion.
    */
    public void setCounterClockwise() {
    clockwise = false;
    }

    }
    [/highlight]

    this is the DLLNode class I'm using
    [highlight = java]

    public class DLLNode<T> {
    private DLLNode<T> back;
    private DLLNode<T> forward;
    private T info;

    public DLLNode(T info) {
    this.info = info;
    this.back = null;
    this.forward = null;
    }

    public void setInfo(T info)
    // Sets info of this LLNode.
    {
    this.info = info;
    }

    public T getInfo()
    // Returns info of this LLONode.
    {
    return info;
    }

    public void setBack(DLLNode<T> back)
    // Sets back link of this DLLNode.
    {
    this.back = back;
    }

    public DLLNode<T> getBack()
    // Returns back link of this DLLNode.
    {
    return back;
    }

    public void setForward(DLLNode<T> forward)
    // Sets forward link of this DLLNode;
    {
    this.forward = forward;
    }

    public DLLNode<T> getForward()
    // Returns forward link of this DLLNode.
    {
    return forward;
    }
    }
    [/highlight]

    Any advice or pointers would be great. Thank you.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: trouble with add and remove method on doubly linked list

    double posted here

Similar Threads

  1. Help with a doubly linked circular list
    By TeamRival in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 10:59 PM
  2. Generic Doubly Linked List
    By javapenguin in forum What's Wrong With My Code?
    Replies: 23
    Last Post: October 23rd, 2010, 03:13 PM
  3. Sorted Doubly linked List
    By student in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 09:52 PM
  4. [SOLVED] Update on Doubly Linked List
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2010, 07:19 PM
  5. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 11:48 AM

Tags for this Thread