CS 46 B Introduction to Data Structures July
CS 46 B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www. cs. sjsu. edu/~mak
Quizzes for July 23 o Quiz 19 July 23 16. 3 Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 2
Homework #8 Solution o Requirements n n o Print all the unique words in Gettysburg. Address. txt in alphabetical order. Use classes from the Java Collections framework. Which classes to use? n unique: Some kind of set, either Hash. Set or Tree. Set. A set does not admit duplicate entries. n alphabetical order: Tree. Set A Tree. Set stores its entries in sorted order. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 3
Homework #8 Solution, cont’d o Read the words into a Tree. Set object: private static final String FILE_NAME = "Gettysburg. Address. txt"; in = new Scanner(new File(FILE_NAME)); in. use. Delimiter("[^A-Za-z]"); Tree. Set<String> words = new Tree. Set<String>(); while (in. has. Next()) { String word = in. next(). to. Lower. Case(); if (word. length() > 0) words. add(word); } If the word is already in the set, the add operation is ignored. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 4
Homework #8 Solution, cont’d o Now print the words in the set. n They’re in order because it’s a Tree. Set. private static final int WORDS_PER_LINE = 8; int counter = 0; for (String word : words) { System. out. print(word); if (++counter < WORDS_PER_LINE) System. out. print(" "); else { counter = 0; System. out. println(); } } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak Demo 5
Linked List Implementation o To understand better the Linked. List class in the Java Collections framework, let’s implement a simplified version. o We will also implement an iterator class. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 6
A Node Class o A linked list is made up of nodes, so we’ll need a Node class. n It will be an inner class of the Linked. List class. public class Linked. List {. . . class Node { public Object data; public Node next; }. . . } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 7
The Linked List Class o We want to keep a reference to the first node of the linked list. n o Initialize first to null in the constructor to signify an empty list. A getter method get. First() returns the data in the first node. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 8
The Linked List Class, cont’d public class Linked. List { private Node first; public Linked. List() { first = null; } public Object get. First() { if (first == null) { throw new No. Such. Element. Exception(); } return first. data; }. . . } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 9
Add the First Element public void add. First(Object element) { Node new. Node = new Node(); new. Node. data = element; new. Node. next = first; first = new. Node; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 10
Remove the First Element public Object remove. First() { if (first == null) { throw new No. Such. Element. Exception(); } Object element = first. data; first = first. next; return element; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 11
The Iterator Class o Another inner class of our linked list class. o We will implement five methods only: n n next() has. Next() add() remove() set() Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 12
The Iterator Class, cont’d o Remember to think of an iterator as a cursor between elements. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 13
The Iterator Class, cont’d public class Linked. List {. . . public List. Iterator list. Iterator() { return new Linked. List. Iterator(); }. . . class Linked. List. Iterator implements List. Iterator { position refers to the private Node position; private Node previous; currently visited node private boolean is. After. Next; public Linked. List. Iterator() { position = null; previous = null; is. After. Next = false; }. . . previous refers to the last node before that is. After. Next keeps track of when the next() method has been called } } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 14
Advance the Iterator public Object next() { if (!has. Next()) { throw new No. Such. Element. Exception(); } previous = position; // Remember for remove is. After. Next = true; if (position == null) { It’s the first element. position = first; } else { position = position. next; It’s the next element. } return position. data; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 15
Advance the Iterator, cont’d public boolean has. Next() { if (position == null) { return first != null; Check the first element. } else { return position. next != null; } Is there a next element? } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 16
Remove an Element o Use the iterator to remove an element from the middle of the linked list. o Remember that a list iterator removes the last element that it visited. n Therefore, you must first call next() to visit an element. n That element is the one that’s removed. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 17
Remove an Element, cont’d We’re going to remove this node. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 18
Remove an Element, cont’d Why can’t we call remove() again? Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 19
Remove an Element, cont’d public void remove() { if (!is. After. Next) { throw new Illegal. State. Exception(); } if (position == first) { remove. First(); } else { previous. next = position. next; } position = previous; is. After. Next = false; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 20
Add an Element o Use the iterator to add (insert) an element to the middle of the linked list. n n You add an element before the cursor. Afterward, the cursor is after the added element. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 21
Add an Element, cont’d Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 22
Add an Element, cont’d Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 23
Add an Element, cont’d public void add(Object element) { if (position == null) { add. First(element); position = first; } else { Node new. Node = new Node(); new. Node. data = element; new. Node. next = position. next; position. next = new. Node; position = new. Node; } is. After. Next = false; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 24
Set a Value o Use the iterator to set or change an element value of the linked list. n Set the value of the element just visited. n Therefore, you must first call next() to visit an element. n That’s the element whose value will be set. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 25
Set a Value, cont’d public void set(Object element) { if (!is. After. Next) { throw new Illegal. State. Exception(); } position. data = element; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 26
Break Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 27
Efficiency of Linked List Operations Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 28
Remove the Last Element Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 29
A Doubly Linked List o Each node now refers to both the next node and the previous node. public class Linked. List { private Node first; private Node last; . . . class Node { public Object data; public Node next; public Node previous; }. . . } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 30
Add the First Element public void add. First(Object element) { Node new. Node = new Node(); new. Node. data = element; new. Node. next = first; new. Node. previous = null; if (first == null) { We just added the last = new. Node; very first element. } else { first. previous = new. Node; } first = new. Node; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 31
Remove the First Element public Object remove. First() { if (first == null) { throw new No. Such. Element. Exception(); } Object element = first. data; first = first. next; if (first == null) { last = null; The list is now empty. } else { first. previous = null; } return element; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 32
Add the Last Element public void add. Last(Object element) { Node new. Node = new Node(); new. Node. data = element; new. Node. next = null; new. Node. previous = last; if (last == null) { We just added the first = new. Node; very first element. } else { last. next = new. Node; } last = new. Node; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 33
Remove the Last Element public Object remove. Last() { if (last == null) { throw new No. Such. Element. Exception(); } Object element = last. data; last = last. previous; if (last == null) { The list is now empty. first = null; } else { last. next = null; } return element; } Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 34
A Bidirectional Iterator o In a doubly-linked list, the iterator can move forward and backward. Linked. List lst = new Linked. List(); lst. add. Last("A"); lst. add. Last("B"); lst. add. Last("C"); List. Iterator iter = lst. list. Iterator(); // The iterator is before the first element |ABC iter. next(); // Returns “A”; the iterator is after the first element A|BC iter. next(); // Returns “B”; the iterator is after the second element AB|C iter. previous(); // Returns “B”; the iterator is after the first element A|BC o Method previous() returns the element after the cursor position. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 35
A Bidirectional Iterator, cont’d List. Iterator iter = lst. list. Iterator(); // The iterator is before the first element |ABC iter. next(); // Returns “A”; the iterator is after the first element A|BC iter. next(); // Returns “B”; the iterator is after the second element AB|C iter. previous(); // Returns “B”; the iterator is after the first element A|BC Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 36
Remove an Element Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 37
Homework #9 o Reverse the elements of a singly-linked list using an iterative method and a recursive method. o Example n n o Original list: [A B C D] Reversed list: [D C B A] You must reverse the linked list in place. n You may not copy the elements to an array or an array list. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 38
Homework #9, cont’d o You are given a linked list class Abbreviated. Linked. List that has a private inner Node class, a private field first, and a public method add. First(). o You will add the iterative reverse() method: public void reverse() o You will add the recursive reverse() method: private Node reverse(Node p) n Parameter p refers to the starting node of the list where reversal is to begin. Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 39
Homework #9, cont’d o There is a public driver method for the recursive reverse() method: public void reverse. Recursive() { first = reverse(first); } o There is no iterator class. o Sample output: Original list: [A B C D] Iteratively reversed: [D C B A] Original list: [A B C D] Recursively reversed: [D C B A] Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 40
Homework #9, cont’d o Draft: Iterative version only. n n n o Codecheck: http: //codecheck. it/codecheck/files/15072108363747 y 0 fo 08 ar 6 knsd 11 a 5 wjak Canvas: Homework 9 Draft Due: Friday, July 24 at 11: 59 PM Final: Replace your iterative method with your recursive method. n n n Codecheck: http: //codecheck. it/codecheck/files/1507210846 ccvdhl 3 kluu 0 qf molsx 1 y 2 h 1 i Canvas: Homework 9 Final Due: Monday, July 27 at 11: 59 PM Computer Science Dept. Summer 2015: July 21 CS 46 B: Introduction to Data Structures © R. Mak 41
- Slides: 41