Download presentation
Presentation is loading. Please wait.
1
HW-6 Deadline Extended to April 27th
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 . Sign-up for the make-up exam slot that works for you.
2
Data Structures 2 2
3
Typical Operations on Lists
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
4
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)
5
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 1) Change String to ints in the sample code. sample client 5 5
6
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) 6 6
7
Queues 7
8
Queue API Iterator<Key> iterator()
return an iterator over the keys enqueue dequeue length Hypothetical Netflix queue 8
9
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) enQueue(Item) { Add item after the last element of the array; } Item deQueue(Item) { Remove and return the first element in the array; head tail 9
10
Arraylist Implementation (Coding Demo)
11
How Do We Implement a Circular Queue?
Add elements 1 … n The (n+1)st element overwrites the 1st element Update head and tail modulo the capacity (n). 11
12
Stacks 12 12
13
Last-In, First-Out (LIFO) List
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 What does it do? Reads in a sequence of strings and prints them in reverse order. Top of Stack 13 13
14
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. JVM is a stack based machine (ala postscript) 14 14
15
Stack<Item> Stack; //create an empty stack
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 JVM is a stack based machine (ala postscript) 15 15
16
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]. it was the best 1 2 3 4 5 6 7 8 9 a[] N all methods are one-liners! explain a[N++] and a[--N] 16 16
17
Arraylist Implementation (Coding Demo)
18
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 18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.