DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Java Doubly Circular Linked List

  1. #1
    Join Date
    Nov 2005
    Posts
    10

    Java Doubly Circular Linked List

    any one know if insertLeft is correct? thanks (it should insrt node given value to left of another given value, if other node not found,inserts left of the header..thanks

    import java.awt.*;
    import hsa.Console;

    public class cdlltest
    {
    static Console c;

    public static void main (String[] args)
    {
    c = new Console ();
    CircularList cl = new CircularList (10);
    c.println (cl.toString ());


    }
    }
    /* class Node
    Fields
    data- integer of the Node's data
    left- the Node to the left of the Node
    right- the Node to the right of the Node
    methods
    constructor

    */
    class Node
    {
    public int data;
    public Node left;
    public Node right;

    public Node (int data)
    {
    left = null;
    right = null;
    this.data = data;

    }


    public String toString ()
    {
    String s = "";
    s += data;
    return s;
    }


    /*REMVOE BEFORE HAND IN!*/
    // public String toString ()
    // {
    // String s = "";
    // s += data + " ";
    // if (right != null)
    // s += "" + right;
    // return s;
    // }
    }

    //travel through list c =l.r while(c!=l) c=c.r
    /* class CircularList

    */

    /* class CircularList
    Fields
    Node list-
    Methods
    constructor

    */
    class CircularList
    {
    Node list;
    public CircularList (int x)
    {
    list = new Node (x);
    list.right = list;
    list.left = list;
    }


    public CircularList ()
    {
    this.list = new Node (0);
    }


    public int numNodes ()
    {
    return list.data;
    }


    public void insertLeft (int x)
    {
    Node r = new Node (x);
    boolean done = false;
    Node c = list.right;
    while (c != list)
    {
    if (c.data == x)
    {
    c.left = r;
    c.left.right = r;
    done = true;
    }
    c = c.right;
    }
    if (!done)
    list.left = r;
    }


    public String toString ()
    {
    String s = "";
    Node c = list.right;
    while (c != list)
    {
    System.out.println ("IM IN!!");
    s += c.toString ();
    c = c.right;
    }
    return s;

    }


    public void insertLeft (int x, int leftOf)

    {

    Node r = new Node (x);

    boolean done = false;

    Node c = list.right;

    while (c != list)
    {

    if (c.data == leftOf)

    {

    r.left = c.left;

    r.right = c;

    c.left = r;

    }

    }

    if (!done)

    {

    r.left = c.left;

    r.right = c;

    c.left = r;

    }

    }
    }

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    Your insertLeft function looks a bit strange. To insert something into a doubly linked list, there are several things that you need to remember to do. Imagine you have a list:
    A C
    A.next refers the the reference to the next node of A, and A.prev refers to the previous node of A (in this case C). Imagine you want to insert B between A and C.

    Code:
    Object next = A.next;
    B.next = next;
    B.prev = A;
    A.next = B;
    next.prev = B;
    Hope this helps.
    ~evlich

Similar Threads

  1. Has Sun Given Up on the Desktop?
    By Lori Piquet in forum Talk to the Editors
    Replies: 114
    Last Post: 10-10-2002, 06:01 AM
  2. ListBot Going Out of Business
    By Larry Rebich in forum vb.announcements
    Replies: 1
    Last Post: 06-28-2001, 01:22 PM
  3. Swap items in two way linked list - Help!
    By LordByte in forum Java
    Replies: 3
    Last Post: 05-29-2001, 08:28 AM
  4. Tree, Linked list and data structures
    By Hemant in forum VB Classic
    Replies: 0
    Last Post: 10-11-2000, 03:38 AM
  5. java list pointers - need help
    By abs in forum Java
    Replies: 0
    Last Post: 09-28-2000, 11:55 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center