Chapter 20 Lists, Stacks, Queues, and Priority Queues

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
John Hurley Cal State LA
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L16 (Chapter 22) Java Collections.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures Using C++ 2E
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Chapter 18 Java Collections Framework
Data structures Abstract data types Java classes for Data structures and ADTs.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
9/27/2016IT 1791 Abstraction A tool (concept) to manage complexity Hide irrelevant details; focus on the features needed Primitive date types are already.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Data Structure By Amee Trivedi.
G64ADS Advanced Data Structures
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
Stacks Chapter 5.
Chapter 19 Java Data Structures
Chapter 15 Lists Objectives
John Hurley Cal State LA
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Abstraction A tool (concept) to manage complexity
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Chapter 17 Object-Oriented Data Structures
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
structures and their relationships." - Linus Torvalds
CS313D: Advanced Programming Language
List Data Structure.
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
CSC 1052 Stacks 1/11/2019.
Collections Not in our text.
Recall What is a Data Structure Very Fundamental Data Structures
Collections Framework
Introduction to Data Structure
Dynamic allocation (continued)
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
structures and their relationships." - Linus Torvalds
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Chapter 20 Lists, Stacks, Queues, and Priority Queues Dr. Clincy - Lecture

What is Data Structure? A data structure is a collection of data organized in some fashion. The structure not only stores data, but also supports operations for accessing and manipulating the data. By using the more appropriate data structure for your program, it will make it more efficient From an OO and Java perspective, a data structure is also known as a container or container object. A container or container object is an object that stores other objects (i.e. data or elements) – this is also called a collection – more specifically, collection interface. So in essence, to define and create a data structure is the same as defining and creating an instance of a class – because a class for a data structure is allowing the ability to store data and manipulate the data (i.e. search, insertion, and deletion). Dr. Clincy - Lecture

Java Collection Framework hierarchy We have already covered a data structure called an ArrayList class Java has several more data structures or collections such as: lists, vectors, stacks, queues, priority queues, sets, and maps. These data structures or collections are known as the Java Collections Framework. The Java Collections Framework defines two types of containers Collection: used for storing a collection of elements (covered in this chapter) Map: used for storing key/value pairs (covered in a later chapter) Types of collections Sets: store a group of nonduplicate elements Lists: store an ordered collection of elements Stacks: store objects that are processed in a LIFO (last in first out) fashion Queues: store ojects that are processed in a FIFO (first in first out) fashion Priority Queues: store objects that are processed in the order of their priorities. Dr. Clincy - Lecture

Java Collection Framework hierarchy, cont. Set and List are subinterfaces of Collection. Dr. Clincy - Lecture

The Collection Interface The Collection interface is for manipulating a collection of objects. Dr. Clincy - Lecture

The List Interface A list stores elements in a sequential order, and allows the user to specify where the element is stored. The user can access the elements by index. Dr. Clincy - Lecture

The List Interface, cont. Dr. Clincy - Lecture

The List Iterator Dr. Clincy - Lecture

ArrayList and LinkedList The ArrayList class and the LinkedList class are concrete implementations of the List interface. Which of the two classes you use depends on your specific needs. If you need to support random access through an index without inserting or removing elements from any place other than the end, ArrayList offers the most efficient collection. If, however, your application requires the insertion or deletion of elements from any place in the list, you should choose LinkedList. A list can grow or shrink dynamically. An array is fixed once it is created. If your application does not require insertion or deletion of elements, the most efficient data structure is the array. Dr. Clincy - Lecture

java.util.ArrayList Dr. Clincy - Lecture

java.util.LinkedList Dr. Clincy - Lecture

Example: Using ArrayList and LinkedList This example creates an array list filled with numbers, and inserts new elements into the specified location in the list. The example also creates a linked list from the array list, inserts and removes the elements from the list. Finally, the example traverses the list forward and backward. TestArrayAndLinkedList Run Dr. Clincy - Lecture

The Comparator Interface Sometimes you want to compare the elements of different types. The elements may not be instances of Comparable or are not comparable. You can define a comparator to compare these elements. To do so, define a class that implements the java.util.Comparator interface. The Comparator interface has the compare method for comparing two objects. Dr. Clincy - Lecture

The Comparator Interface public int compare(Object element1, Object element2) Returns a negative value if element1 is less than element2, a positive value if element1 is greater than element2, and zero if they are equal. GeometricObjectComparator TestComparator Run Dr. Clincy - Lecture

Other Comparator Examples SortStringByLength Run SortStringIgnoreCase Run Dr. Clincy - Lecture

The Collections Class The Collections class contains various static methods for operating on collections and maps, for creating synchronized collection classes, and for creating read-only collection classes. Dr. Clincy - Lecture

The Collections Class UML Diagram Dr. Clincy - Lecture

Case Study: Multiple Bouncing Balls MultipleBounceBall Run Dr. Clincy - Lecture

The Vector and Stack Classes The Java Collections Framework was introduced with Java 2. Several data structures were supported prior to Java 2. Among them are the Vector class and the Stack class. These classes were redesigned to fit into the Java Collections Framework, but their old-style methods are retained for compatibility. This section introduces the Vector class and the Stack class. Dr. Clincy - Lecture

The Vector Class In Java 2, Vector is the same as ArrayList, except that Vector contains the synchronized methods for accessing and modifying the vector. None of the new collection data structures introduced so far are synchronized. If synchronization is required, you can use the synchronized versions of the collection classes. These classes are introduced later in the section, “The Collections Class.” Dr. Clincy - Lecture

The Vector Class, cont. Dr. Clincy - Lecture

The Stack Class The Stack class represents a last-in-first-out stack of objects. The elements are accessed only from the top of the stack. You can retrieve, insert, or remove an element from the top of the stack. Dr. Clincy - Lecture

Queues and Priority Queues A queue is a first-in/first-out data structure. Elements are appended to the end of the queue and are removed from the beginning of the queue. In a priority queue, elements are assigned priorities. When accessing elements, the element with the highest priority is removed first. Dr. Clincy - Lecture

The Queue Interface Dr. Clincy - Lecture

Using LinkedList for Queue Dr. Clincy - Lecture

The PriorityQueue Class PriorityQueueDemo Run Dr. Clincy - Lecture

Case Study: Evaluating Expressions Stacks can be used to evaluate expressions. Evaluate Expression Run Dr. Clincy - Lecture

Algorithm Phase 1: Scanning the expression The program scans the expression from left to right to extract operands, operators, and the parentheses. 1.1. If the extracted item is an operand, push it to operandStack. 1.2. If the extracted item is a + or - operator, process all the operators at the top of operatorStack and push the extracted operator to operatorStack. 1.3. If the extracted item is a * or / operator, process the * or / operators at the top of operatorStack and push the extracted operator to operatorStack. 1.4. If the extracted item is a ( symbol, push it to operatorStack. 1.5. If the extracted item is a ) symbol, repeatedly process the operators from the top of operatorStack until seeing the ( symbol on the stack. Phase 2: Clearing the stack Repeatedly process the operators from the top of operatorStack until operatorStack is empty. Dr. Clincy - Lecture

Example Dr. Clincy - Lecture