Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures 1 1.

Similar presentations


Presentation on theme: "Data Structures 1 1."— Presentation transcript:

1 Data Structures 1 1

2 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)

3 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)

4 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

5 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)

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

7 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

8 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

9 Queues

10 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

11 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

12 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

13 Arraylist Implementation (Coding Demo)

14 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


Download ppt "Data Structures 1 1."

Similar presentations


Ads by Google