TraversingTheLinkedList_part1-dsf.2

TraversingTheLinkedList_part1-dsf.2 - Traversing the Linked...

Info iconThis preview shows pages 1–9. 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 Dawn Finney 1 CS1316: Traversing the Linked List Question Suppose we have a linked list of books in a library and the books are defined as follows: public class BookElement { private String bookDetails = ""; private BookElement next = null; public String getDetails() {return this.bookDetails;} public BookElement getNext() {return this.next;} public void setDetails(String details) {this.bookDetails = details;} public void setNext(BookElement be) {this.next = be;} } Write a method length() that returns the length of the list that starts at this by filling in the code template. 2 CS1316: Traversing the Linked List Code Template public int length() { // Hint: Initialize number here. // You also need a current element, starting at this. // Hint: Then comes the while loop for the traversal while (_________________________________________) { // Hint: do something inside the loop that helps you count. // Hint: do something so that the loop does not repeat forever. } return number; } 3 CS1316: Traversing the Linked List Provided Solution public int length() { // Hint: Initialize number here. int number = 1; // You need a current element, starting at this. BookElement current = this; // Hint: Then comes the while loop for the traversal while (current.getNext() != null ) { // Hint: do something inside the loop that helps you count. number++; // Hint: do something so that the loop does not repeat forever. current = current.getNext(); } return number; } 4 CS1316: Traversing the Linked List Testing the Solution: Setting up the Test List node 1 node 2 node 3 null BookElement node1 = new BookElement(); BookElement node2 = new BookElement(); BookElement node3 = new BookElement(); node1.setNext(node2); node2.setNext(node3); System.out.println(node1.length()); 5 CS1316: Traversing the Linked List Testing the Solution: Running through the Method node 1 node 2 node 3 null public int length() { int number = 1; BookElement current = this; while (current.getNext() != null ) { number++; current = current.getNext(); } return number; } 6 number = 1 Now we have a variable number that will act as our counter and we initialize it to 1. CS1316: Traversing the Linked List Testing the Solution: Running through the Method node 1 node 2 node 3 null public int length() { int number = 1; BookElement current = this; while (current.getNext() != null ) { number++; current = current.getNext(); } return number; } 7 number = 1 current Now we have a variable current acting as a pointer to the current node we are considering in the list. CS1316: Traversing the Linked List Testing the Solution: Running through the Method node 1 node 2 node 3 null public int length() { int number = 1; BookElement current = this; while (current.getNext() != null ) { number++; current = current.getNext(); } return number; } 8 number = 1 current Because the expression...
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 / 32

TraversingTheLinkedList_part1-dsf.2 - Traversing the Linked...

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

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