Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.

Slides:



Advertisements
Similar presentations
Chapter 24 Lists, Stacks, and Queues
Advertisements

Chapter 9: Data Structures I
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CISH 4960 Introduction to Programming Lecture 101 Introduction to Programming Lecture 10 Recursion/Data Structures Tom Blough
Chapter 12: Data Structures
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 3 Introduction to Collections – Stacks Modified
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Data structures Abstract data types Java classes for Data structures and ADTs.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stacks And Queues Chapter 18.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Copyright © 2002 Pearson Education, Inc. Slide 1.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Collections (Stack, queue, tree, graph). References as Links References can be used to create a variety of linked structures, such as a linked list.
Java Software Solutions Foundations of Program Design Seventh Edition
Data Structure By Amee Trivedi.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Chapter 12: Data Structures
Stacks and Queues.
Data Structures and Database Applications Abstract Data Types
Chapter 12 Collections.
Stacks, Queues, and Deques
Chapter 13 Collections.
Chapter 12 Collections.
Stacks, Queues, and Deques
Presentation transcript:

Chapter 12 Collections

© 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage other objects Chapter 12 focuses on:  the concept of a collection  separating the interface from the implementation  dynamic data structures

© 2004 Pearson Addison-Wesley. All rights reserved12-3 Outline Data Structures Dynamic Representations Queues and Stacks Trees and Graphs The Java Collections API

© 2004 Pearson Addison-Wesley. All rights reserved12-4 Abstraction Our data structures should be abstractions That is, they should hide unneeded details We want to separate the interface of the structure from its underlying implementation This helps manage complexity and makes it possible to change the implementation without changing the interface

© 2004 Pearson Addison-Wesley. All rights reserved12-5 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different from the meaning of the static modifier Arrays are static; once you define the number of elements it can hold, the number doesn’t change A dynamic data structure grows and shrinks at execution time as required by its contents A dynamic data structure is implemented using links

© 2004 Pearson Addison-Wesley. All rights reserved12-6 Object References Recall that an object reference is a variable that stores the address of an object A reference also can be called a pointer References often are depicted graphically: student John Smith

© 2004 Pearson Addison-Wesley. All rights reserved12-7 References as Links Object references can be used to create links between objects Suppose a Student class contains a reference to another Student object John Smith Jane Jones

© 2004 Pearson Addison-Wesley. All rights reserved12-8 References as Links References can be used to create a variety of linked structures, such as a linked list: studentList

© 2004 Pearson Addison-Wesley. All rights reserved12-9 Intermediate Nodes The objects being stored should not be concerned with the details of the data structure in which they may be stored For example, the Student class should not have to store a link to the next Student object in the list Instead, we can use a separate node class with two parts: 1) a reference to an independent object and 2) a link to the next node in the list The internal representation becomes a linked list of nodes

© 2004 Pearson Addison-Wesley. All rights reserved12-10 Magazine Collection Let’s explore an example of a collection of Magazine objects The collection is managed by the MagazineList class, which has an private inner class called MagazineNode Because the MagazineNode is private to MagazineList, the MagazineList methods can directly access MagazineNode data without violating encapsulation

© 2004 Pearson Addison-Wesley. All rights reserved12-11 MagazineRack.java //******************************************************************* // MagazineRack.java Author: Lewis/Loftus // // Driver to exercise the MagazineList collection. //******************************************************************* public class MagazineRack { // // Creates a MagazineList object, adds several magazines to the // list, then prints it. // public static void main (String[] args) { MagazineList rack = new MagazineList(); rack.add (new Magazine("Time")); rack.add (new Magazine("Woodworking Today")); rack.add (new Magazine("Communications of the ACM")); rack.add (new Magazine("House and Garden")); rack.add (new Magazine("GQ")); System.out.println (rack); } }

© 2004 Pearson Addison-Wesley. All rights reserved12-12 //******************************************************************* // MagazineList.java Author: Lewis/Loftus // // Represents a collection of magazines. //******************************************************************* public class MagazineList { private MagazineNode list; // // Sets up an initially empty list of magazines. // public MagazineList() { list = null; } // // Creates a new MagazineNode object and adds it to the // end of the linked list. // public void add (Magazine mag) { MagazineNode node = new MagazineNode (mag); MagazineNode current; if (list == null) list = node; else { current = list; while (current.next != null) current = current.next; current.next = node; } } // // Returns this list of magazines as a string. // public String toString () { String result = ""; MagazineNode current = list; while (current != null) { result += current.magazine + "\n"; current = current.next; } return result; } //***************************************************************** // An inner class that represents a node in the magazine // list. The public variables are accessed by the // MagazineList class. //***************************************************************** private class MagazineNode { public Magazine magazine; public MagazineNode next; // // Sets up the node // public MagazineNode (Magazine mag) { magazine = mag; next = null; } } }

© 2004 Pearson Addison-Wesley. All rights reserved12-13 Magazine.java //******************************************************************** // Magazine.java Author: Lewis/Loftus // // Represents a single magazine. //******************************************************************** public class Magazine { private String title; // // Sets up the new magazine with its title. // public Magazine (String newTitle) { title = newTitle; } // // Returns this magazine as a string. // public String toString () { return title; } }

© 2004 Pearson Addison-Wesley. All rights reserved12-14 Magazine Collection A method called insert could be defined to add a node anywhere in the list, to keep it sorted, for example

© 2004 Pearson Addison-Wesley. All rights reserved12-15 Magazine Collection A method called delete could be defined to remove a node from the list (Figure 12.3 here)

© 2004 Pearson Addison-Wesley. All rights reserved12-16 Other Dynamic List Representations It may be convenient to implement as list as a doubly linked list, with next and previous references list

© 2004 Pearson Addison-Wesley. All rights reserved12-17 Other Dynamic List Implementations It may be convenient to use a separate header node, with a count and references to both the front and rear of the list count: 4 front rear list

© 2004 Pearson Addison-Wesley. All rights reserved12-18 Other Dynamic List Implementations A linked list can be circularly linked in which case the last node in the list points to the first node in the list If the linked list is doubly linked, the first node in the list also points to the last node in the list The representation should facilitate the intended operations and should make them easy to implement

© 2004 Pearson Addison-Wesley. All rights reserved12-19 Classic Data Structures Classic linear data structures include queues and stacks Classic nonlinear data structures include trees, binary trees, graphs, and digraphs

© 2004 Pearson Addison-Wesley. All rights reserved12-20 Queues A queue is similar to a list but adds items only to the rear of the list and removes them only from the front It is called a FIFO data structure: First-In, First-Out Analogy: a line of people at a bank teller’s window enqueue dequeue

© 2004 Pearson Addison-Wesley. All rights reserved12-21 Queues We can define the operations for a queue  enqueue - add an item to the rear of the queue  dequeue (or serve) - remove an item from the front of the queue  empty - returns true if the queue is empty As with our linked list example, by storing generic Object references, any object can be stored in the queue Queues often are helpful in simulations or any situation in which items get “backed up” while awaiting processing

© 2004 Pearson Addison-Wesley. All rights reserved12-22 Queues A queue can be represented by a singly-linked list; it is most efficient if the references point from the front toward the rear of the queue A queue can be represented by an array, using the mod operator (%) to “wrap around” when the end of the array is reached and space is available at the front of the array

© 2004 Pearson Addison-Wesley. All rights reserved12-23 Stacks A stack ADT is also linear, like a list or a queue Items are added and removed from only one end of a stack It is therefore LIFO: Last-In, First-Out Analogies: a stack of plates in a cupboard, a stack of bills to be paid, or a stack of hay bales in a barn

© 2004 Pearson Addison-Wesley. All rights reserved12-24 Stacks Stacks often are drawn vertically: poppush

© 2004 Pearson Addison-Wesley. All rights reserved12-25 Stacks Some stack operations:  push - add an item to the top of the stack  pop - remove an item from the top of the stack  peek (or top) - retrieves the top item without removing it  empty - returns true if the stack is empty A stack can be represented by a singly-linked list; it doesn’t matter whether the references point from the top toward the bottom or vice versa A stack can be represented by an array, but the new item should be placed in the next available place in the array rather than at the end of the array

© 2004 Pearson Addison-Wesley. All rights reserved12-26 Stacks The java.util package contains a Stack class Like ArrayList operations, the Stack operations operate on Object references //******************************************************************** // Decode.java Author: Lewis/Loftus // // Demonstrates the use of the Stack class. //******************************************************************** import java.util.*; public class Decode { // // Decodes a message by reversing each word in a string. // public static void main (String[] args) { Scanner scan = new Scanner (System.in); Stack word = new Stack(); String message; int index = 0; System.out.println ("Enter the coded message:"); message = scan.nextLine(); System.out.println ("The decoded message is:"); while (index < message.length()) { // Push word onto stack while (index < message.length() && message.charAt(index) != ' ') { word.push (new Character(message.charAt(index))); index++; } // Print word in reverse while (!word.empty()) System.out.print (((Character)word.pop()).charValue()); System.out.print (" "); index++; } System.out.println(); } }

© 2004 Pearson Addison-Wesley. All rights reserved12-27 Trees and Binary Trees A tree is a non-linear data structure that consists of a root node and potentially many levels of additional nodes that form a hierarchy Nodes that have no children are called leaf nodes Nodes except for the root and leaf nodes are called internal nodes (Figure 12.8 here)

© 2004 Pearson Addison-Wesley. All rights reserved12-28 Trees and Binary Trees A binary tree is defined recursively. Either it is empty (the base case) or it consists of a root and two subtrees, each of which is a binary tree Binary trees and trees typically are represented using references as dynamic links, though it is possible to use fixed representations like arrays

© 2004 Pearson Addison-Wesley. All rights reserved12-29 Graphs and Digraphs A graph is a non-linear structure Unlike a tree or binary tree, a graph does not have a root Any node in a graph can be connected to any other node by an edge Analogy: the highway system connecting cities on a map (Figure 12.9 here)

© 2004 Pearson Addison-Wesley. All rights reserved12-30 Graphs and Digraphs In a directed graph or digraph, each edges has a specific direction. Edges with direction sometimes are called arcs Analogy: airline flights between airports (Figure here)

© 2004 Pearson Addison-Wesley. All rights reserved12-31 Graphs and Digraphs Both graphs and digraphs can be represented using dynamic links or using arrays. As always, the representation should facilitate the intended operations and make them convenient to implement

© 2004 Pearson Addison-Wesley. All rights reserved12-32 Collection Classes The Java standard library contains several classes that represent collections, often referred to as the Java Collections API Their underlying implementation is implied in the class names such as ArrayList and LinkedList Several interfaces are used to define operations on the collections, such as List, Set, SortedSet, Map, and SortedMap

© 2004 Pearson Addison-Wesley. All rights reserved12-33 Summary Chapter 12 has focused on:  collections  Abstract Data Types (ADTs)  dynamic structures and linked lists  queues and stacks  non-linear data structures  predefined collection classes