TraversingTheLinkedList_part2-dsf

TraversingTheLinkedList_part2-dsf - Traversing the Linked...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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.

Page1 / 34

TraversingTheLinkedList_part2-dsf - Traversing the Linked...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online