Results 1 to 8 of 8
  1. #1
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Question Insert() Method Help

    Hello,

    I am attempting to make my insert method in the form of a circular list. Each time a new item is inserted it is to take on the tag as "current" Here is my method currently.
    Java Code:
    public void insert(long dd) {
     
    		Link newLink = new Link(dd);
     
    		if (isEmpty()) {
    			current = newLink;
    		}
     
    		else {
     
    			newLink.next = current.next;
     
    		}
     
    		current.next = newLink;
     
    		count++;
    	}
    However, I have my doubts that this is not entirely circular, I think I need to somehow edit this to include "previous" maybe? Also my current output after inserting 5 integers is as follows

    Java Code:
    Insert 10
    List: 10 
     
    Insert 22
    List: 10 22 
     
    Insert 5
    List: 10 5 22 
     
    Insert 44
    List: 10 44 5 22 
     
    Insert 32
    List: 10 32 44 5 22
    However, I would prefer, each new item is inserted after the last "current"

    So it should read

    Java Code:
    Insert 10
    List: 10 
     
    Insert 22
    List: 10 22 
     
    Insert 5
    List: 10  22 5 
     
    Insert 44
    List: 10 22 5 44
     
    Insert 32
    List: 10 22 5 44 32

    Can someone please help me make this circular as well as insert after one another instead of just after 10 each time?

    *Also posted at: New To Java - Insert() Method Help (looking for helpful reply tonight)

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

    Default

    Link Class seems has 2 variable, Link next and int count
    i suggest you can add 2 more variable, Link head and Link prev,
    so you can insert new Link to head.prev
    Last edited by mtyoung; 02-26-2009 at 04:46 AM.

  3. #3
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    I cannot create a head and tail in the link class, this would not be a true circular link list if I did so, that would just have the tail around around to the head.

    I may only use the reference "current" to traverse through the link list.

    the class "list" may have as many references as possible however.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by VinceGuad View Post
    Hello,
    Each time a new item is inserted it is to take on the tag as "current"
    For this to be the case your insert() method should always assign a new value to current - which it is not doing at the moment.

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

    Default

    create a header or tail, circular linked list still a "circular" linked list
    header/tail are reference for append new element to list
    even adding the header, you should use current traverse through the linked list.

    if you do not like to add tail, how about adding Link prev, becoming a doubly circular linked list

    i like discussion...
    Last edited by mtyoung; 02-26-2009 at 04:59 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    For anybody else who isn't sure what a "circular list" is - do you mean what wikipedia calls a "circularly linked list"? If so these beasts can be either singly or doubly linked. That is, you can have a "previous" instance variable but you need not.

    (btw, there's lots of discussion on Wikipedia of the algorithms. under the heading "linked list".)

  7. #7
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    my instance should be a singly linked list

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

Similar Threads

  1. Help with sql insert
    By jmorris in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 07:05 PM
  2. SQL Insert Help!!!!
    By shaungoater in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 03:14 AM
  3. How to insert graph in java
    By valery in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:38 PM
  4. How do insert a Graphic
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:30 AM
  5. how to insert tables into JTextPane
    By osval in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 09:11 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
  •