|
-
November 2nd, 2005, 06:37 AM
#1
linked list help, urgent
I have created my own linked list, is all the method correct, i dont know how to do the indexOf method and toString, please show me how to do it, and can anyone tell me how can i test this linked list, in a simple way, thanks.
class MyLinkedList
{
private int count;
ListNode = header;
ListNode = temp;
public MyLinkedList()
{
header = new ListNode(null);
count = 0;
}
public boolean isEmpty()
{
return header.next == null;
}
//addElement(Object obj), void.
//Adds a new Object at the end of the list.
public void addElement(Object obj)
{
ListNode node = new ListNode(obj);
if(isEmpty() == true)
{
header.next = node;
count++;
}
else
{
temp = header;
while (temp.getnext()!=null)
{
temp=temp.getnext();
}
temp.next=node;
count++;
}
}
//insertElementAt(Object obj, int index), void
//Inserts a new Object at the position indicated by the index. Objects after that position
//are moved up by one index position.
public void insertElementAt(Object obj, int index)
{
if (index>count || index<1)
{
return null;
}
else
{
ListNode node = new ListNode(obj);
count++;
temp = header;
for(int i = 1; i < index; i++)
{
temp=temp.getnext();
}
node.next=temp.next
temp.next=node;
}
}
// setElementAt(Object obj, int index), void
// Inserts a new Object at the position indicated by the index by overwriting any existing
// Object at that position.
public void setElementAt(Object obj, int index)
{
if (index>count || index<1)
{
return null;
}
else
}
temp = header;
for(int i = 1; i < index; i++)
{
temp=temp.getnext();
}
temp.next.element=obj;
}
}
// elementAt(int index), returns an Object.
// Returns the Object located at the position indicated by the index.
public Object elementAt(int index)
{
if (index > count || index < 1)
{
return null;
}
else
{
temp = header;
for(int i = 1; i < index; i++)
{
temp=temp.getnext();
}
return temp.element;
}
}
//size(), returns the number of elements occupied, int.
public int size()
{
return count;
}
// remove(int index) : void
// Removes the Object located at the position indicated by the index. All Objects after that
// position are moved down by one index position
public void remove(int index)
{
if (index>count || index<1)
{
return null;
}
else
}
count--;
temp = header;
for(int i = 1; i < index; i++)
{
temp=temp.getnext();
}
temp.next=temp.next.next;
}
}
public indexOf(Object obj)
//indexOf(Object obj), returns the index of the first occurrence of the Object obj.
//how to do this
// toString(), This standard method should return a single String which includes the
// output of the toString() methods of any Objects stored in the NewVector object. Each should
// be separated by a new line character.
class ListNode
{
Object element;
ListNode next;
ListNode(Object theElement)
{
this(theElement,null);
}
ListNode(Object theElement, ListNode n)
{
element = theElement;
next = n;
}
public ListNode getNext()
{
return this.next;
}
}
}
-
November 2nd, 2005, 08:49 PM
#2
Re: linked list help, urgent
I have created my own linked list
Your first mistake. Use the collections classes provided by Java. Any new functionality you'd like to add should be included in a class you derrive from one of java's collection classes.
I know how to build. What to build is a completely different story.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|