140 likes | 159 Views
Data Structures. 1. 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)
E N D
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)
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. Parameterize stack by a single type. You need to import the library: import java.util.ArrayList; Generics (Dr. Java Code) 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 sample client 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. ArrayList<Integer> list= newArrayList<Integer>(); list.add(17);// autobox (int -> Integer) int a = list.get(i); // autounbox (Integer -> int) 8
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. 10
Queue API Iterator<Key> iterator() return an iterator over the keys enqueue dequeue length publicstaticvoid 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()); }
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? tail head
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[] 0 1 2 3 4 5 6 7 8 9 capacity = 10 head tail