Midterm Review1.5

Midterm Review1.5 - The Array List ADT public interface...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Array List ADT public interface IndexList<E> { /** Returns the number of elements in this list */ public int size(); /** Returns whether the list is empty. */ public boolean isEmpty(); /** Inserts an element e to be at index I, shifting all elements after this. */ public void add(int I, E e) throws IndexOutOfBoundsException; /** Returns the element at index I, without removing it. */ public E get(int i) throws IndexOutOfBoundsException; /** Removes and returns the element at index I, shifting the elements after this. */ public E remove(int i) throws IndexOutOfBoundsException; /** Replaces the element at index I with e, returning the previous element at i. */ public E set(int I, E e) throws IndexOutOfBoundsException; } CSE 2011 Prof. J. Elder - 21 - Last Updated: 1/7/10 10:14 AM Performance In the array based implementation The space used by the data structure is O(n) size, isEmpty, get and set run in O(1) time add and remove run in O(n) time In an add operation, when the array is full, instead of throwing an exception, we could replace the array with a larger one. In fact java.util.ArrayList implements this ADT using extendable arrays that do just this. CSE 2011 Prof. J. Elder - 22 - Last Updated: 1/7/10 10:14 AM Doubling Strategy Analysis We replace the array k = log2 n times The total time T(n) of a series of n add(o) operations is proportional to n + 1 + 2 + 4 + 8 + …+ 2k = n + 2k + 1 1 = 2n 1 geometric series Thus T(n) is O(n) 2 The amortized time of an add operation is O(1)! 4 1 1 8 1 r n +1 Recall: ri = 1r i =0 n CSE 2011 Prof. J. Elder - 23 - Last Updated: 1/7/10 10:14 AM Stacks Chapter 5.1 CSE 2011 Prof. J. Elder - 24 - Last Updated: 1/7/10 10:14 AM The Stack ADT The Stack ADT stores arbitrary objects Auxiliary stack operations: Insertions and deletions follow the last-in first-out scheme object top(): returns the last inserted element without removing it Think of a spring-loaded plate dispenser integer size(): returns the number of elements stored Main stack operations: boolean isEmpty(): indicates whether no elements are stored push(object): inserts an element object pop(): removes and returns the last inserted element CSE 2011 Prof. J. Elder - 25 - Last Updated: 1/7/10 10:14 AM ...
View Full Document

Ask a homework question - tutors are online