Chapter 24 Dispensers and dictionaries. This chapter discusses n Dictionaries n Dispensers u stacks u queues u priority queues.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Stacks, Queues, and Linked Lists
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
Fundamentals of Python: From First Programs Through Data Structures
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
TCSS 342, Winter 2005 Lecture Notes
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
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)
© 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 in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
C++ STL CSCI 3110.
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,
Queues Tonga Institute of Higher Education. Definitions Queue - A data structure that stores a number of items. The first item entered into a queue is.
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.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Holding Your Objects Generics and type-safe containers One of the problems of using pre-Java SE5 containers was that the compiler allowed.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
UNIVERSAL COLLEGE OF ENGG. AND TECH. 3 RD IT. QUEUE ( data structure)
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Stack and Queues Part 2. Priority Queues Priority Queues (cont’) A priority queue is a more specialized data structure than a stack or a queue. However,
Data-structure-palooza Checkout DataStructures from SVN.
Linear Data Structures
C++ Review STL CONTAINERS.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Computer Engineering Rabie A. Ramadan Lecture 6.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
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.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Queues.
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.
Stacks Chapter 5.
Chapter 15 Lists Objectives
Stack and Queue APURBO DATTA.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Abstract Data Types (ADTs)
Presentation transcript:

Chapter 24 Dispensers and dictionaries

This chapter discusses n Dictionaries n Dispensers u stacks u queues u priority queues

Dispensers n A dispenser is a container that restricts access to its elements. n Only one element in the container can be accessed or removed. n This element is called the “current element.”

Dispensers (cont.) n A dispenser has three essential features: adding, removing, accessing. void add (Object obj) Add the specified element to this dispenser. void remove () Remove the current element from this dispenser. Object get () The current element of this dispenser.

Dispensers (cont.) n You can combine access and removal into one operation. Object dispenseItem () obj = dispenser.dispenseItem(); is equivalent to obj = dispenser.get(); dispenser.remove(); n The former is preferred

Stacks n stack: a dispenser in which the current element is the container element most recently added to the container. n Also known as last-in first-out, or LIFO lists.

Stacks (cont.) The features get, add, and remove traditionally are named top, push, and pop.

Stack implementations n We use the same bridge pattern for implementing dispensers as we did for implementing lists. u We provide the container with an implementation component so that we can extend the class to meet the needs of an application, independent of its implementations.

Stack implementations (cont.) n In an array-based implementation: n This permits pushing and popping to be done in constant time.

Stack implementations (cont.) n In a linked implementation: n Again, pushing and popping can be done in constant time.

Translating Stack features n In order to build Stack features into BoundedList or LinkedList, we could use an adapter class to translate the specifications of an existing class to those required. n We “wrap” the existing class in the new class.

Translating Stack features (cont.) n We can also adapt an existing class by extending it.

Queues n queue:a dispenser in which the current element is the container element least recently added to the container. n Also known as first-in first-out, or FIFO lists. The fundamental dispenser features are front, append, and remove ; append and remove traditionally are named enqueue and serve.

Queue implementations n If a LinkedList maintains references to both ends of the list, elements can be added to either end in constant time. n Constant time deletes can be done only from the front of the list. Therefore, we make the front of LinkedList the front of the queue. n An adapter class can be defined easily in much the same way as was done for Stacks.

Queue implementations (cont.) n If we attempt to use the class BoundedList to implement queues, we encounter a problem: u Adding and removing elements requires shuffling the list, and takes linear time.

Queue implementations (cont.) n Viewing an array as a circular structure permits all queue methods to operate in constant time.

Queue implementations (cont.) n The queue occupies a set of contiguous array elements, and “circulates” through the array as items are added and removed.

Queue implementations (cont.) n The relationship between the two indexes is the same for both the full queue and the empty queue. (rear+1)%n == front

Priority queue n priority queue: a dispenser in which the current element is a largest container element with respect to some given ordering. n A largest item with respect to the ordering is referred to as the highest priority item. The dispenser features highest, add, and remove ; add and remove are traditionally named enqueue and serve.

PriorityQueue implementations n A way to implement a priority queue is with the class OrderedList. n If we base a PriorityQueue implementation on either an array-based or linked implementation of OrderedList,  add will be linear  highest and remove can be made constant time operations u With an array-based implementation, we can find an element’s position in log n time.

Dictionaries n dictionary: a container in which the elements are accessed by key. n Dictionary entries have a key and an associated value. i.e. a key-value pair. u Example: English dictionary: an English word is the key, and the definition is the value. n We can also consider the key to be an attribute of the entry, rather than a separate component. u Example: telephone directory has name (key), address and number.

Dictionaries (cont.) n A key must be provided to access or delete an item. n We assume keys are unique.

Dictionary implementations n We can build straightforward implementations of dictionaries with a List whose elements are key-value pairs. The method get and remove can simply search the List to locate the item with the given key. java.util defines an interface Map that serves as a superclass for dictionary variants.

We’ve covered n Dispensers. u stacks u queues u priority queues n Dictionaries. n Implementations using BoundedList and LinkedList. n Method performance.

Glossary