Chapter 13 Collections.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Chapter 9: Data Structures I
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.
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 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
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.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Chapter 3 Introduction to Collections – Stacks Modified
Reference: Vinu V Das, Principles of Data Structures using C and C++
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Data Structures Using Java1 Chapter 4 Linked Lists.
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.
Nodes & You Understanding the changes that are happening to your code.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stacks And Queues Chapter 18.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
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
STACKS & QUEUES for CLASS XII ( C++).
Stack: a Linked Implementation
CHAPTER 4: Linked Structures
Section 2.6 Linked List StringLog ADT Implementation
Chapter 4 Linked Structures.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Lectures linked lists Chapter 6 of textbook
CS2006- Data Structures I Chapter 5 Linked Lists I.
Building Java Programs
Sequences and Iterators
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Chapter 12: Data Structures
Data Structures Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective.
Stacks and Queues.
Data Structures and Database Applications Linked Lists
Data Structures and Database Applications Abstract Data Types
Chapter 12 Collections.
Top Ten Words that Almost Rhyme with “Peas”
CMSC 341 Lecture 5 Stacks, Queues
Topic 11 Linked Lists -Joel Spolsky
Chapter 18: Linked Lists.
Building Java Programs
Linked Lists.
Lecture 7: Linked List Basics reading: 16.2
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Dynamic Data Structures and Generics
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Chapter 12 Collections.
Dynamic Data Structures and Generics
Introduction to Data Structure
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
slides created by Marty Stepp
Topic 11 Linked Lists -Joel Spolsky
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

Chapter 13 Collections

Collections A collection is an object that helps us organize and manage other objects the concept of a collection separating the interface from the implementation dynamic data structures linked lists queues and stacks trees and graphs generics

Outline Collections and Data Structures Dynamic Representations Linear Structures (Queues & Stacks) Non-Linear Structures (Trees & Graphs) The Java Collections API

http://www.journaldev.com/1260/java-collections-framework-tutorial

Collections A collection is an object that serves as a repository for other objects A collection provides services for adding, removing, clear, isEmpty, size, and otherwise managing the elements it contains Sometimes the elements in a collection are ordered, sometimes they are not Sometimes collections are homogeneous, containing all the same type of objects, and sometimes they are heterogeneous

Abstraction Collections can be implemented in many different ways Collections 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

Abstract Data Types 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 In one sense, as long as the ADT fulfills the promises of the interface, it doesn't matter how the ADT is implemented Objects are a good programming mechanism to create ADTs because their internal details are encapsulated

Outline Collections and Data Structures Dynamic Representations Linear Structures (Queues & Stacks) Non-Linear Structures (Trees & Graphs) The Java Collections API

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 size 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 object references as links int[] anArray= new int[10];

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 40725 3.58

References as Links Object references can be used to create links between objects Suppose a class contains a reference to another object of the same class: class Node { String info; Node next; }

References as Links References can be used to create a variety of linked structures, such as a linked list:

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, use a separate node class with two parts: a reference to an independent object a link to the next node in the list The internal representation becomes a linked list of nodes

Magazine Collection Let’s explore an example of a collection of Magazine objects, managed by the MagazineList class, which has a private inner class called MagazineNode See MagazineRack.java See MagazineList.java See Magazine.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); }

continue //***************************************************************** // 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; }

//******************************************************************* // 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; } continue

continue //---------------------------------------------------------------- // 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; }

continue //---------------------------------------------------------------- // 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;

//******************************************************************** // 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;

Output Time Woodworking Today Communications of the ACM House and Garden GQ //******************************************************************* // 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); }

Inserting a Node A node can be inserted into a linked list with a few reference changes:

Quick Check Write code that inserts newNode after the node pointed to by current.

Quick Check Write code that inserts newNode after the node pointed to by current. newNode.next = current.next; current.next = newNode;

Deleting a Node Likewise, a node can be removed from a linked list by changing the next pointer of the preceding node:

Other Dynamic Representations It may be convenient to implement a list as a doubly linked list, with next and previous references:

Other Dynamic Representations Another approach is to use a separate header node, with a count and references to both the front and rear of the list: