Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ANNOUNCEMENTS HW-6 Deadline Extended to April 27th Final Exam – May 2nd at 7-9 PM in Olsson Hall Room 120 Exam will be 33% longer than Exams 1 and 2 33% of the material are from those covered in Exams 1 and 2 In-Class final exam review session - April 20th Make-up exam slots and criteria will be announced via e-mail. Sign-up for the makeup exam slot that works for you. 1 Data Structures 2 Data Structures Arrays Fixed size Lists A collection of a variable number of items Typical Operations on Lists 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 3 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) 4 Generics Generics. Parameterize the datatype used in the data structure. You need to import the library: import java.util.ArrayList; 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 5 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= new ArrayList<Integer>(); list.add(17); // autobox (int -> Integer) int a = list.get(i); // autounbox (Integer -> int) 6 Queues 7 Queue API Iterator<Key> return an iterator over the keys iterator() enqueue dequeue length 8 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) head tail enQueue(Item) { Add item after the last element of the array; } Item deQueue(Item) { Remove and return the first element in the array; } 9 Arraylist Implementation (Coding Demo) 10 How Do We Implement a Circular Queue? Add elements 1 … n st element overwrites the 1st element The (n+1) Update head and tail modulo the capacity (n). 11 Stacks 12 Stack Last-In, First-Out (LIFO) List Items are added to the top of the stack (push) Items are removed from the top of the stack (pop) push pop Top of Stack 13 Stack Applications Real world applications. Parsing in a compiler. Undo in a word processor. Back button in a Web browser. Implementing function calls in a compiler. 14 Stack API Stack<Item> Stack; //create an empty stack boolean IsEmpty(); //check whether the stack is empty void push(Item); //push an item onto the stack Item pop(); //pop the stack 15 Stack: Array Implementation Array implementation of a stack. Use array a[] to store N items on stack. Use the last index of the array to keep track of the stack top push() add new item at a[N]. pop() remove item from a[N-1]. a[] it was the best 0 1 2 3 4 5 6 7 8 9 N 16 Arraylist Implementation (Coding Demo) 17 Summary Stacks and queues are fundamental ADTs. Array implementation. Linked list implementation. Many applications. NEXT CLASS: Review for Final Exam NO CLASS on April 22nd and 27th but will hold office-hours during those times in Olsson 236B 18