Data Structures 1 1.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

Queues.
Topic 9 The Queue ADT.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
4.3 Stacks and Queues Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Basics.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
Click to edit Master text styles Stacks Data Structure.
Guest Speaker - Cryptography Wednesday (Apr. 22) – Prof. abhi shelat.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Collections (Stack, queue, tree, graph). References as Links References can be used to create a variety of linked structures, such as a linked list.
The Queue ADT.
CMSC 202 ArrayList Aug 9, 2007.
Building Java Programs
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
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.
Queue ADT (Abstract Data Type) N …
Stacks and Queues.
(like an array on steroids)
Queues.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Abstraction A tool (concept) to manage complexity
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Stacks and Queues.
Introduction to Data Structure
HW-6 Deadline Extended to April 27th
Stacks and Queues.
Basic Data Types Queues
structures and their relationships." - Linus Torvalds
Queues.
Stacks and Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
ArrayLists.
CSC 143 Queues [Chapter 7].
Building Java Programs
CMSC 202 ArrayList Aug 9, 2007.
Building Java Programs
Dynamic Data Structures and Generics
Stacks and Queues CLRS, Section 10.1.
CMSC 202 ArrayList Aug 9, 2007.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Array Lists CSE 1310 – Introduction to Computers and Programming
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
CS 200 Objects and ArrayList
Dynamic Data Structures and Generics
Introduction to Data Structure
CE 221 Data Structures and Algorithms
slides created by Alyssa Harding
Generics, Stack, Queue Based on slides by Alyssa Harding
CS 200 Objects and ArrayList
Getting queues right … finally (?)
structures and their relationships." - Linus Torvalds
Stacks and Queues.
Data Structures & Programming
Presentation transcript:

Data Structures 1 1

Computer is a machine that manipulates information Dataypes Computer is a machine that manipulates information Need to organize, manipulate, and use information This is done at different levels in the system Bits (machine-level representation) Built-in datatypes (language-level representation) Arrays, objects (user-defined datatypes)

Convenient to separate specification from implementation Data Structures Convenient to separate specification from implementation Abstract Data Types Hide internal implementation of the data type Allows correct use of the datatype from its specification Allows composition of other useful data representations from the simpler ones (Data Structures)

Examples of Data Structures Arrays Drawback? Lists A collection of a variable number of items Typical Operations of List ADT Add an item to the list Remove an item from the list Read an item from the list Check whether the list is empty Get the current size of the list

Lists Can Be Organized in Different Ways Linked List Linear sequence of elements Queue Remove the item least recently added. First-In, First-Out (FIFO) Stack Remove the item most recently added. Last-In, First-Out (LIFO)

What About the Data Part? Would like to be generic - support all data types Java’s solution: The ArrayList Class

Generics (Dr. Java Code) Generics. Parameterize stack by a single type. You need to import the library: import java.util.ArrayList; parameterized type parameterized type ArrayList<Apple> list= new ArrayList<Apple>(); Apple a = new Apple(); Orange b = new Orange(); list.add(a); list.add(b); // compile-time error a = list.get(0); // returns an Apple object 1) Change String to ints in the sample code. sample client 7 7

Autoboxing Generic ArrayList implementation. Only permits reference types. Wrapper type. Each primitive type has a wrapper reference type. Ex: Integer is wrapper type for int. Autoboxing. Automatic cast from primitive type to wrapper type. Autounboxing. Automatic cast from wrapper type to primitive type. syntactic sugar: casts are still done behind the scenes (hidden cost) ArrayList<Integer> list= new ArrayList<Integer>(); list.add(17); // autobox (int -> Integer) int a = list.get(i); // autounbox (Integer -> int) 8 8

Queues

Queue Applications Some applications. iTunes playlist. Data buffers (iPod, TiVo). Asynchronous data transfer (file IO, pipes, sockets). Dispensing requests on a shared resource (printer, processor). Simulations of the real world. Traffic analysis. Waiting times of customers at call center. Determining number of cashiers to have at a supermarket. CD players – use buffer to make skip-free ("shock protection") Lincoln tunnel: can you make a left turn off of 10th avenue onto 31st? Just changed and now 90 minute delays. Someone didn't simulate the consequences (properly). Graph processing (stay tuned for breadth first search). 10 10

Queue API Iterator<Key> iterator() return an iterator over the keys enqueue dequeue length public static void main(String[] args) { Queue<String> q = new Queue<String>(); q.enqueue(”No Country For Old Men"); q.enqueue(”Dark Knight"); q.enqueue(”Pulp Fiction"); q.enqueue(”Dude, Where Is My Car?"); while(!q.isEmpty()) StdOut.println(q.dequeue()); } Hypothetical Netflix queue

Array Implementation of Queues With an array q[]. Head (Keep track of the front of the queue) Tail (Keep track of the back of the queue) How do we enqueue elements? How do we remove elements? head tail

Arraylist Implementation (Coding Demo)

Queue: “Circular” Array Implementation Array implementation of a queue. Use array q[] to store items on queue. enqueue(): add new object at q[tail]. dequeue(): remove object from q[head]. Update head and tail modulo the capacity. the best of times q[] 1 2 3 4 5 6 7 8 9 head tail capacity = 10