Click to See Complete Forum and Search --> : learning insert(Comparable obj) in LinkedLIst


ami26
June 21st, 2004, 02:48 AM
Hi to all of u,

Just bear with me, i am new to this forum.
I created a class and i want to use
public vois insert(comparable obj) in a LinkedList//ascending order.
Could somebody give me an example...
Thank you.
:confused:

Here is what i have done:

import java.util.*;


interface SortedList extends Comparable {

public void insert(Comparable data);

public void print();
}

public class LinkedList {

public class IntNode {
LinkedList list = new LinkedList();
Comparable item;
private IntNode head; IntNode link;

public void insert(Comparable data) {

IntNode ptr; // address of node to insert before.
IntNode prev; // address of node to insert after.
prev = null;
ptr = head;

// locate postion for insertion
???//missing code
}

}

}
Thanks for taking the time to read my post

cjard
June 21st, 2004, 06:19 AM
your linkedlist does not implement the sorted list interface.

to insert in an order-wise fashion into a linked list, you must declare 2 pointers, one to the first item in the list (A), one to the second (B). first deal with the special case that the item should go at the start of the list, else, check if the item is less than the B pointer. if it is not, move both pointers on by one and test again. as soon as you get to a point where the item is less than the B pointer:
A pointer's next item should be the new item
the new item's next item should be the B pointer.

if you look at the diagram i've drawn, it appears more easy. first, the special case, is the value less than the a?
then you can go into a loop (look for the repetition in the drawing = a loop)

ps; this is how you shopuld write programs... use a pen and paper and DRAW this..

cjard
June 21st, 2004, 06:21 AM
you might ask why you need node A?

well, linked lists are normally 1 direction only, so you need to keep reference of the one behind where you are so you can set the previous node's NEXT to the new thing, and then set the NEXT of the new thing to the current node (the B node..)

dlorde
June 21st, 2004, 06:27 AM
You really need to take a careful look at the whole thing, line by line. There's little point posting a tangle of code lines that don't compile and don't make sense.

You have a SortedList interface that extends the Comparable interface. This just won't work. An interface can be implemented, but not extended. But why does the list implement the Comparable interface anyway? Do you really want to compare two SortedLists?
What is the relationship of SortedList to the LinkedList class?
You can't declare class IntNode public within the LinkedList class - it's not legal Java.
Why does IntNode construct an instance of LinkedList?
etc.

When you've sorted out these issues and have a code skeleton that actually compiles, post it up here with your questions.

As for inserting the items in order, you have to iterate through the existing items in the list until you find an item that compares greater (or less, depending which direction you go) than the item you're inserting. You then point the new item's link pointer to that item, and the next item's link pointer to the new item (or vice-versa if you're going the other way) :ehh:

A rainstorm had triggered a flash-flood... he was three hours in from the cave entrance, and the water was already chest high and rising fast...