• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to insert an object to a linkedlist ADT?

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, i found the bug. I forgot to new the LinkedList object.
reply
    Bookmark Topic Watch Topic
  • New Topic