1 / 14

Data Structures

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)

ellism
Download Presentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures 1

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

  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. ArrayList<Integer> list= newArrayList<Integer>(); list.add(17);// autobox (int -> Integer) int a = list.get(i); // autounbox (Integer -> int) 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. 10

  11. 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()); }

  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? tail head

  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[] 0 1 2 3 4 5 6 7 8 9 capacity = 10 head tail

More Related