GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes How to insert an object to a linkedlist ADT? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to insert an object to a linkedlist ADT?" Watch "How to insert an object to a linkedlist ADT?" New topic
Author

How to insert an object to a linkedlist ADT?

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
Here are 3 sources code from a data structure book:

For the public void insert( Object x, LinkedListItr p )
{
if( p != null && p.current != null )
p.current.next = new ListNode( x, p.current.next );
}
It requires me to pass Object x and LinkedListItr p.
But, in the LinkedListItr class, the constructor requires the ListNode object:
LinkedListItr( ListNode theNode )
{
current = theNode;
}
However, the ListNode object takes Object theElement and ListNode n as parameters:
ListNode( Object theElement, ListNode n )
{
element = theElement;
next = n;
}
Now, I have a String array [100]. How can I insert those 100 objects one by one in the linked list?
I tried the following iteration code:
for( i = 0; i < 100; i++ ){
theList.insert(array[i], theItr ); //*** This has problem!!
printList( theList );
theItr.advance( );
}
It throws a NullPointerException. How can I fix it?
Thanks for help
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
sorry, i found the bug. I forgot to new the LinkedList object.
 
GeeCON Prague 2014
 
subject: How to insert an object to a linkedlist ADT?