© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,

Slides:



Advertisements
Similar presentations
Chapter 9: Data Structures I
Advertisements

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 6: Arrays Java Software Solutions for AP* Computer Science
Data and information These are philosophical categories These are philosophical categories We are not able to give exact definition We are not able to.
CISH 4960 Introduction to Programming Lecture 101 Introduction to Programming Lecture 10 Recursion/Data Structures Tom Blough
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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.
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.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 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.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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’
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
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.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linear Data Structures
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,
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
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
Review Array Array Elements Accessing array elements
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 12: Data Structures
Stacks and Queues.
Data Structures and Database Applications Abstract Data Types
Chapter 10: Non-linear Data Structures
Chapter 12 Collections.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Stacks, Queues, and Deques
Chapter 13 Collections.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Chapter 12 Collections.
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Presentation transcript:

© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2006 by John Lewis, William Loftus, and Cara Cocking. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. *AP is a registered trademark of The College Entrance Examination Board which was not involved in the production of, and does not endorse, this product.

© 2006 Pearson Education 2 Linear Data Structures  Now we can now explore some convenient techniques for organizing and managing information  Chapter 9 focuses on: collections Abstract Data Types (ADTs) dynamic structures and linked lists queues and stacks

© 2006 Pearson Education 3 Collections  A collection is an object that serves as a repository for other objects  A collection usually provides services such as adding, removing, and otherwise managing the elements it contains  Sometimes the elements in a collection are ordered, sometimes they are not  Sometimes collections are homogeneous, sometimes the are heterogeneous

© 2006 Pearson Education 4 Abstract Data Types  Collections can be implemented in many different ways  An abstract data type (ADT) is an organized collection of information and a set of operations used to manage that information  The set of operations defines the interface to the ADT  As long as the ADT fulfills the promises of the interface, it doesn't really matter how the ADT is implemented  Objects are a perfect programming mechanism to create ADTs because their internal details are encapsulated

© 2006 Pearson Education 5 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

© 2006 Pearson Education 6 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, Map, and Queue

© 2006 Pearson Education 7 Generics  The Java Collection classes are implemented as generic types  We give the type of object a collection will hold when we create the collection, like this: LinkedList myStringList = new LinkedList (); LinkedList myBookList = new LinkedList ();  If no type is given, the collection is defined as containing references to the Object class

© 2006 Pearson Education 8 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

© 2006 Pearson Education 9 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

© 2006 Pearson Education 10 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

© 2006 Pearson Education 11 References as Links  References can be used to create a variety of linked structures, such as a linked list: studentList

© 2006 Pearson Education 12 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  See ListNode.java (pg 517) ListNode.java

© 2006 Pearson Education 13 Magazine Collection  Let’s explore an example of a collection of Magazine objects  The collection is managed by the MagazineList class, which contains a linked list of ListNode objects  The data portion of each ListNode is a Magazine object  See ListNode.java (page 517) ListNode.java  See MagazineRack.java (page 521) MagazineRack.java  See MagazineList.java (page 522) MagazineList.java  See Magazine.java (page 524) Magazine.java

© 2006 Pearson Education 14 Inserting a Node  A method called insert could be defined to add a node anywhere in the list, to keep it sorted, for example  Inserting at the front of a linked list is a special case

© 2006 Pearson Education 15 Inserting a Node  When inserting a node in the middle of a linked list, we must first find the spot to insert it  Let current refer to the node before the spot where the new node will be inserted

© 2006 Pearson Education 16 Deleting a Node  A method called delete could be defined to remove a node from the list  Again the front of the list is a special case:  Deleting from the middle of the list:

© 2006 Pearson Education 17 Other Dynamic List Representations  It may be convenient to implement as list as a doubly linked list, with next and previous references list

© 2006 Pearson Education 18 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

© 2006 Pearson Education 19 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

© 2006 Pearson Education 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

© 2006 Pearson Education 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 isEmpty - returns true if the queue is empty  Queues often are helpful in simulations or any situation in which items get “backed up” while awaiting processing  Java provides a Queue interface, which the LinkedList class implements: Queue q = new LinkedList ();

© 2006 Pearson Education 22 Priority Queues  In a priority queue, some elements get to “cut in line”  The enqueue and isEmpty operations behave the same as with normal queues  The dequeue operation removes the element with the highest priority  See AfterSchoolTasks.java (page 530) AfterSchoolTasks.java  See Task.java (page 531) Task.java

© 2006 Pearson Education 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

© 2006 Pearson Education 24 Stacks  Stacks often are drawn vertically: poppush

© 2006 Pearson Education 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 - retrieves the top item without removing it isEmpty - 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

© 2006 Pearson Education 26 Stacks  The Stack class is part of the Java Collections API and thus is a generic class Stack strStack = new Stack ();  See Decode.java (page 533) Decode.java  See ArrayStack.java (page 534) ArrayStack.java

© 2006 Pearson Education 27 Summary  Chapter 9 has focused on: collections Abstract Data Types (ADTs) dynamic structures and linked lists queues and stacks