Traversing the Linked List - Swikis on...

32
Traversing the Linked List Dawn Finney 1 CS1316: Traversing the Linked List

Transcript of Traversing the Linked List - Swikis on...

Page 1: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Traversing the Linked List

Dawn Finney

1CS1316: Traversing the Linked List

Page 2: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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.

2CS1316: Traversing the Linked List

Page 3: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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;

}

3CS1316: Traversing the Linked List

Page 4: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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 traversalwhile (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;

}

4CS1316: Traversing the Linked List

Page 5: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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());

5CS1316: Traversing the Linked List

Page 6: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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

Page 7: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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

Page 8: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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 current.getNext() != null evaluates to true, we enter the loop.

CS1316: Traversing the Linked List

Page 9: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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;

}

9

number = 2

current

Add 1 to number.

CS1316: Traversing the Linked List

Page 10: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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;

}

10

number = 2

current

We have finished looking at the current so change it to the next node.

CS1316: Traversing the Linked List

Page 11: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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;

}

11

number = 2

current

Because the expression current.getNext() != null evaluates to true, the loop continues

CS1316: Traversing the Linked List

Page 12: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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;

}

12

number = 3

current

Add 1 to number.

CS1316: Traversing the Linked List

Page 13: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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;

}

13

number = 3

current

We have finished looking at the current so change it to the next node.

CS1316: Traversing the Linked List

Page 14: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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;

}

14

number = 3

current

Because the expression current.getNext() != null evaluates to false, we do not enter the loop again

CS1316: Traversing the Linked List

Page 15: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney 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;

}

15

number = 3

current

Return the number we have calculated.

CS1316: Traversing the Linked List

Page 16: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Flaws within the Solution

• It is okay to presume that number starts at 1, because we know that we have at least 1 node in the list. We know that there is at least 1 node in the list, because we are within the method so we know that this is not null or we would have received a NullPointerException and gotten kicked out before we even entered the method.

• However, with the presumption that the number starts at 1, we cannot have current set to this, because we have already counted this by starting at 1. The current node should start at this.getNext().

• Regardless of whether number starts at 0 or 1, current starts at this or this.getNext(), the while conditional should be current != null so that we will actually visit each node.

16CS1316: Traversing the Linked List

Page 17: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Better Solution

public int length() {// Hint: Initialize number here.int number = 0;// You need a current element, starting at this.BookElement current = this;// Hint: Then comes the while loop for the traversalwhile (current != 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;

}

17CS1316: Traversing the Linked List

Page 18: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Better 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.getLength());

18CS1316: Traversing the Linked List

Page 19: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

19

number = 0

Now we have a variable number that will act as our counter and we initialize it to 0.

CS1316: Traversing the Linked List

Page 20: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

20

number = 0

current

Just like before we have a variable current acting as a pointer to the current node we are considering in the list.

CS1316: Traversing the Linked List

Page 21: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

21

number = 0

current

Because the expression current != null evaluates to true, we enter the loop.

CS1316: Traversing the Linked List

Page 22: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

22

number = 1

current

Add 1 to number.

CS1316: Traversing the Linked List

Page 23: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

23

number = 1

current

We have finished looking at the current so change it to the next node.

CS1316: Traversing the Linked List

Page 24: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

24

number = 1

current

Because the expression current != null evaluates to true, the loop continues.

CS1316: Traversing the Linked List

Page 25: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

25

number = 2

current

Add 1 to number.

CS1316: Traversing the Linked List

Page 26: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

26

number = 2

current

We have finished looking at the current so change it to the next node.

CS1316: Traversing the Linked List

Page 27: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

27

number = 2

current

Because the expression current != null evaluates to true, the loop continues.

CS1316: Traversing the Linked List

Page 28: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

28

number = 3

current

Add 1 to number.

CS1316: Traversing the Linked List

Page 29: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

29

number = 3

current

We have finished looking at the current so change it to the next node.

CS1316: Traversing the Linked List

Page 30: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

30

number = 3

current

Because the expression current != null evaluates to false, the loop ends.

CS1316: Traversing the Linked List

Page 31: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Testing the Solution: Running through the Method

node 1 node 2 node 3 null

public int length() {

int number = 0;

BookElement current = this;

while (current != null ) {

number++;

current = current.getNext();

}

return number;

}

31

number = 3

current

Return the number we have calculated.

CS1316: Traversing the Linked List

Page 32: Traversing the Linked List - Swikis on coweb.cccoweb.cc.gatech.edu/.../336/TraversingTheLinkedList... · Traversing the Linked List Dawn Finney CS1316: Traversing the Linked List

Why is the Second Solution Better?

• We actually physically visit each node before incrementing the counter.

• This traversal is more reusable when considering other problems with traversals of linked lists.

32CS1316: Traversing the Linked List