This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CS631 Homework 4 solutions 1. Convert LinkedList class to use object rather than integer. This is posted to the class web page under Lab and Example Code, Homework folder. The key was replacing int by Object in the LinkedList and node classes. Also key were how to put integers in and out of the list: • Putting them in – box a primitive int in a new Integer. L.addFirst(new Integer(37)); • Taking them out – use the fact all Objects have a “toString” method so we don’t really have to know that they are integers to print them. System.out.print( " " + current.getInfo().toString()); If we did, we would have to cast them back to Integer with this line: int I = ((Integer)current.getInfo()).intValue(); 2. Page 283, #4. Delete the maximum value from a linked list. Posted to the class web page under Lab and Example Code, Homework folder. Yes, you can delete an item on a single traversal of the list with the expense of maintaining a following pointer, called “maxprev” in the code. In general, you want to save time by minimizing the number of times pointer, called “maxprev” in the code....
View Full Document
- Spring '09
- Trigraph, Array data structure, th em