This preview shows pages 1–8. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Traversing the Linked List 2 Dawn Finney CS1316: Traversing the Linked List 33 Question Suppose we have a linked list of rooms in a building, and the rooms are defined as follows: public class RoomElement { private int area = 0; private RoomElement next = null; public int getArea() {return this.area;} public RoomElement getNext() {return this.next;} public void setArea(int a) {this.area = a;} public void setNext(RoomElement re) {this.next = re;} } Write a method totalArea() that returns the total area of all the rooms in a linked list starting from the first one in the list. The method will be called on the starting position. CS1316: Traversing the Linked List 34 Student Solution public int totalArea() { int total = this.getArea(); RoomElement current = this; while ( current.getNext() != null) { total = total + current.getArea(); current = current.getNext(); } return total; } 35 CS1316: Traversing the Linked List The question here is if this solution is correct. This solution was fashioned with similar logic to the provided solution to the length() method for the BookElement class. Will the same logic work in this question as well? Testing the Solution: Setting up the Test List node 1 1 node 2 2 node 3 3 null RoomElement node1 = new RoomElement(); RoomElement node2 = new RoomElement(); RoomElement node3 = new RoomElement(); node1.setArea(1); node2.setArea(2); node3.setArea(3); node1.setNext(node2); node2.setNext(node3); System.out.println(node1.totalArea()); 36 CS1316: Traversing the Linked List Testing the Solution: Running through the Method node 1 1 node 2 2 node 3 3 null public int totalArea() { int total = this.getArea(); RoomElement current = this; while ( current.getNext() != null) { total = total + current.getArea(); current = current.getNext(); } return total; } 37 CS1316: Traversing the Linked List He initializes total to the area of the first node. total = 1 Testing the Solution: Running through the Method node 1 1 node 2 2 node 3 3 null public int totalArea() { int total = this.getArea(); RoomElement current = this; while ( current.getNext() != null) { total = total + current.getArea(); current = current.getNext(); } return total; } 38 CS1316: Traversing the Linked List Just like before he has a variable current acting as a pointer to the current node he is considering in the list, but have we not seen this same mistake before? Let us explore further. total = 1 current Testing the Solution: Running through the Method node 1 1 node 2 2 node 3 3 null public int totalArea() { int total = this.getArea(); RoomElement current = this; while ( current.getNext() != null) { total = total + current.getArea(); current = current.getNext(); } return total; } 39 CS1316: Traversing the Linked List Because the expression current.getNext != null evaluates to true , we enter the loop. Is this a good expression to use?...
View
Full
Document
This note was uploaded on 01/30/2010 for the course CS 1316 taught by Professor Staff during the Spring '08 term at Georgia Institute of Technology.
 Spring '08
 Staff

Click to edit the document details