{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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
Traversing the Linked List Dawn Finney 1 CS1316: Traversing the Linked List
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full Document Right Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full Document Right Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full Document Right Arrow Icon
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 current.getNext() != null evaluates to true , we enter the loop.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}