![]() |
Assignment 1The Assignmenthttp://learnu.ics.hawaii.edu/~nickles/211_Spring_2005/labs/LinkedListReversal.htmSubmission
RecommendationsWrite DLLNode first.
Write DLList.
Write DLLTest
Help Getting StartedFor those of you who have absolutely no idea of how to start.
DLLNode.java. First, write the DLLNode class as described above. There's examples in the textbook of single-linked Nodes. In DLLNode, you are just building the node data structure; you are not actually connecting anything together. You never actually create a
DLList.java. The only member variable you really need is public boolean add(Object obj){ //we can't just attach obj to our list; //we need to put it in a node first. /* *create a new DLLNode newNode containing obj */ //Now you can add this node to your list. //But there are two cases you need to deal with: // -- when the list is empty, which means head == null // -- you already have some node(s) in the list. if (head == null) { //list is empty /* * Make head point to your new node. * You don't need to change any of newNode's links * because there are no other nodes to point to */ }else { //You need to get to the end of the list //so you can add newNode there. //You only have a reference (head) to //the start of the list, so you'll need a cursor //to move from the start of the list to the end //along the chain of nodes. DLLNode curr = head; //Now curr is pointing to the first node. //And you know curr != null, because if there //was no first node, you wouldn't be in this else //block right now. while (curr.getNext() != null) { //we want to move along the list until curr //points to the last node, just before we fall //off the end of the list and curr goes to null. //(If you get any NullPointerExceptions, it is //because you "fell off the end of the list" //somewhere.) /* * Make curr point to the next node in the list */ } //now curr is pointing at the last node //(since curr.getNext() == null) curr.setNext( /* to what? */ ); /* * finish connecting newNode to the end * of the list. (Remember there's 2 links.) */ } //now we're done adding our node to the list //we need to return true if we successfully added //the element. (Some lists don't allow nulls or //duplicate items, and so would return false if they //were given such an item. We take any kind of item, //so we'll always return true.) return true; } This is only one way to do it. Note that we are not using or maintaining a tail variable in this example. If you needed this help, be sure to trace this code on paper for adding an item to list that already contains 0, 1, 2, and 3 objects. Make sure you know how it works. Note how traversing the list happens. If you need to reach a certain position in the list, you can create a counter, starting at 0, and increment it each time you move along the list to a new node. Then you could stop traversing when you reach the position you need. Grading:Out of 10 points:
You can use this program -- DLLGrader.java -- to check your implementation. This is basically how I will grade your program, though I might change or add a few things. Passing DLLGrader doesn't guarantee that your implementation is completely correct, but it should catch most of the bugs. SolutionZach's Solution. Remember, there's always more than one way to do it; this is just the way I did it. The most important criteria for a program is whether it works correctly. A close second is whether someone else can read (and so maintain) it. |
~ztomasze Index :
TA Details: ICS211:
Assignment 1 http://www2.hawaii.edu/~ztomasze |
Last Edited: 10 Feb 2004 ©2004 by Z. Tomaszewski. |