CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.

Slides:



Advertisements
Similar presentations
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
Advertisements

AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Double-Linked Lists and Circular Lists
The List ADT Textbook Sections
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
CS 46B: Introduction to Data Structures June 25 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
LinkedList Many slides from Horstmann modified by Dr V.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fifteen: An Introduction to Data Structures.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
CS 46B: Introduction to Data Structures June 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
CS 367 Introduction to Data Structures Lecture 5.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
Chapter 15 An Introduction to Data Structures. Chapter Goals To learn how to use the linked lists provided in the standard library To be able to use iterators.
List Interface and Linked List Mrs. Furman March 25, 2010.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Chapter 22 Generic Programming. Chapter Goals To understand the objective of generic programming To be able to implement generic classes and methods To.
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
CS 146: Data Structures and Algorithms June 11 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 15 – An Introduction to Data Structures.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
Linked Lists and Generics Written by J.J. Shepherd.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
CSE 373 Implementing a Stack/Queue as a Linked List
Chapter 20 An Introduction to Data Structures
Chapter 16 – Basic Data Structures
Linked Lists Chapter 6.5, 17 CSCI 3333 Data Structures.
Introduction to Data Structures
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Chapter 15 – An Introduction to Data Structures
Computer Science and Engineering
Collections Framework
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
ITI Introduction to Computing II Lab-12
Presentation transcript:

CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Quizzes for July 23  Quiz 19 July

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Homework #8 Solution  Requirements Print all the unique words in GettysburgAddress.txt in alphabetical order. Use classes from the Java Collections framework.  Which classes to use? unique: Some kind of set, either HashSet or TreeSet. alphabetical order: TreeSet 3 A set does not admit duplicate entries. A TreeSet stores its entries in sorted order.

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Homework #8 Solution, cont’d  Read the words into a TreeSet object: 4 in = new Scanner(new File(FILE_NAME)); in.useDelimiter("[^A-Za-z]"); TreeSet words = new TreeSet (); while (in.hasNext()) { String word = in.next().toLowerCase(); if (word.length() > 0) words.add(word); } private static final String FILE_NAME = "GettysburgAddress.txt"; If the word is already in the set, the add operation is ignored.

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Homework #8 Solution, cont’d  Now print the words in the set. They’re in order because it’s a TreeSet. 5 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(); } } private static final int WORDS_PER_LINE = 8; Demo

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Linked List Implementation  To understand better the LinkedList class in the Java Collections framework, let’s implement a simplified version.  We will also implement an iterator class. 6

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak A Node Class  A linked list is made up of nodes, so we’ll need a Node class. It will be an inner class of the LinkedList class. 7 public class LinkedList {... class Node { public Object data; public Node next; }... }

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak The Linked List Class  We want to keep a reference to the first node of the linked list. Initialize first to null in the constructor to signify an empty list.  A getter method getFirst() returns the data in the first node. 8

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak The Linked List Class, cont’d 9 public class LinkedList { private Node first; public LinkedList() { first = null; } public Object getFirst() { if (first == null) { throw new NoSuchElementException(); } return first.data; }... }

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Add the First Element 10 public void addFirst(Object element) { Node newNode = new Node(); newNode.data = element; newNode.next = first; first = newNode; }

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Remove the First Element 11 public Object removeFirst() { if (first == null) { throw new NoSuchElementException(); } Object element = first.data; first = first.next; return element; }

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak The Iterator Class  Another inner class of our linked list class.  We will implement five methods only: next() hasNext() add() remove() set() 12

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak The Iterator Class, cont’d  Remember to think of an iterator as a cursor between elements. 13

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak The Iterator Class, cont’d 14 public class LinkedList {... public ListIterator listIterator() { return new LinkedListIterator(); }... class LinkedListIterator implements ListIterator { private Node position; private Node previous; private boolean isAfterNext; public LinkedListIterator() { position = null; previous = null; isAfterNext = false; }... } } position refers to the currently visited node previous refers to the last node before that isAfterNext keeps track of when the next() method has been called

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Advance the Iterator 15 public Object next() { if (!hasNext()) { throw new NoSuchElementException(); } previous = position; // Remember for remove isAfterNext = true; if (position == null) { position = first; } else { position = position.next; } return position.data; } It’s the first element. It’s the next element.

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Advance the Iterator, cont’d 16 public boolean hasNext() { if (position == null) { return first != null; } else { return position.next != null; } } Check the first element. Is there a next element?

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Remove an Element  Use the iterator to remove an element from the middle of the linked list.  Remember that a list iterator removes the last element that it visited. Therefore, you must first call next() to visit an element. That element is the one that’s removed. 17

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Remove an Element, cont’d 18 We’re going to remove this node.

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Remove an Element, cont’d 19 Why can’t we call remove() again?

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Remove an Element, cont’d 20 public void remove() { if (!isAfterNext) { throw new IllegalStateException(); } if (position == first) { removeFirst(); } else { previous.next = position.next; } position = previous; isAfterNext = false; }

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Add an Element  Use the iterator to add (insert) an element to the middle of the linked list. You add an element before the cursor. Afterward, the cursor is after the added element. 21

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Add an Element, cont’d 22

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Add an Element, cont’d 23

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Add an Element, cont’d 24 public void add(Object element) { if (position == null) { addFirst(element); position = first; } else { Node newNode = new Node(); newNode.data = element; newNode.next = position.next; position.next = newNode; position = newNode; } isAfterNext = false; }

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Set a Value  Use the iterator to set or change an element value of the linked list. Set the value of the element just visited. Therefore, you must first call next() to visit an element. That’s the element whose value will be set. 25

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Set a Value, cont’d 26 public void set(Object element) { if (!isAfterNext) { throw new IllegalStateException(); } position.data = element; }

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Break 27

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Efficiency of Linked List Operations 28

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Remove the Last Element 29

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak A Doubly Linked List  Each node now refers to both the next node and the previous node. 30 public class LinkedList { 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 46B: Introduction to Data Structures © R. Mak Add the First Element 31 public void addFirst(Object element) { Node newNode = new Node(); newNode.data = element; newNode.next = first; newNode.previous = null; if (first == null) { last = newNode; } else { first.previous = newNode; } first = newNode; } We just added the very first element.

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Remove the First Element 32 public Object removeFirst() { if (first == null) { throw new NoSuchElementException(); } Object element = first.data; first = first.next; if (first == null) { last = null; } else { first.previous = null; } return element; } The list is now empty.

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Add the Last Element 33 public void addLast(Object element) { Node newNode = new Node(); newNode.data = element; newNode.next = null; newNode.previous = last; if (last == null) { first = newNode; } else { last.next = newNode; } last = newNode; } We just added the very first element.

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Remove the Last Element 34 public Object removeLast() { if (last == null) { throw new NoSuchElementException(); } Object element = last.data; last = last.previous; if (last == null) { first = null; } else { last.next = null; } return element; } The list is now empty.

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak A Bidirectional Iterator  In a doubly-linked list, the iterator can move forward and backward.  Method previous() returns the element after the cursor position. 35 LinkedList lst = new LinkedList(); lst.addLast("A"); lst.addLast("B"); lst.addLast("C"); ListIterator iter = lst.listIterator(); // 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 46B: Introduction to Data Structures © R. Mak A Bidirectional Iterator, cont’d 36 ListIterator iter = lst.listIterator(); // 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 46B: Introduction to Data Structures © R. Mak Remove an Element 37

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Homework #9  Reverse the elements of a singly-linked list using an iterative method and a recursive method.  Example Original list: [A B C D] Reversed list: [D C B A]  You must reverse the linked list in place. You may not copy the elements to an array or an array list. 38

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Homework #9, cont’d  You are given a linked list class AbbreviatedLinkedList that has a private inner Node class, a private field first, and a public method addFirst().  You will add the iterative reverse() method:  You will add the recursive reverse() method: Parameter p refers to the starting node of the list where reversal is to begin. 39 public void reverse() private Node reverse(Node p)

Computer Science Dept. Summer 2015: July 21 CS 46B: Introduction to Data Structures © R. Mak Homework #9, cont’d  There is a public driver method for the recursive reverse() method:  There is no iterator class.  Sample output: 40 public void reverseRecursive() { first = reverse(first); } 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 46B: Introduction to Data Structures © R. Mak Homework #9, cont’d  Draft: Iterative version only. Codecheck: knsd11a5wjak knsd11a5wjak Canvas: Homework 9 Draft Due: Friday, July 24 at 11:59 PM  Final: Replace your iterative method with your recursive method. Codecheck: molsx1y2h1i molsx1y2h1i Canvas: Homework 9 Final Due: Monday, July 27 at 11:59 PM 41