Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to insert an object to a linkedlist ADT?

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i found the bug. I forgot to new the LinkedList object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic