L16cs2110fa09-6up - Abstract Data Types(ADTs 2 A method for achieving abstraction for data structures and algorithms ADT = model operations

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
10/22/2009 1 STANDARD ADTS Lecture 16 CS2110 – Fall 2009 Abstract Data Types (ADTs) 2 ± A method for achieving abstraction for data structures and algorithms ± ADT = model + operations y In Java, an interface corresponds well to an ADT ² The interface describes the operations, but says nothing at all about how they are implemented ± Describes what each operation does, but not how it does it ± An ADT is independent of its implementation y Example: Stack interface/ADT public interface Stack { public void push(Object x); public Object pop(); public Object peek(); public boolean isEmpty(); public void clear(); } Queues & Priority Queues 3 ± ADT Queue ± Operations: ³ void add(Object x); ³ Object poll(); ³ Object peek(); y ADT PriorityQueue ² Operations: void insert(Object x); Object getMax(); Object peekAtMax(); boolean isEmpty(); ³ boolean isEmpty(); ³ void clear(); ± Where used: ± Simple job scheduler (e.g., print queue) ± Wide use within other algorithms void clear(); y Where used: ² Job scheduler for OS ² Event-driven simulation ² Can be used for sorting ² Wide use within other algorithms A (basic) queue is “first in, first out”. A priority queue ranks objects: getMax() returns the “largest” according to the comparator interface. Sets 4 ± ADT Set ± Operations: void insert(Object element); boolean contains(Object element); void remove(Object element); boolean isEmpty(); void clear(); for(Object o: mySet) { . .. } ± Where used: ± Wide use within other algorithms ± Note: no duplicates allowed ± A “set” with duplicates is sometimes called a multiset or bag A set makes no promises about ordering, but you can still iterate over it. Dictionaries 5 ± ADT Dictionary (aka Map) ± Operations: ³ void insert(Object key, Object value); ³ void update(Object key, Object value); ³ Object find(Object key); ³ void remove(Object key); ³ boolean isEmpty(); ³ void clear(); ± Think of: key = word; value = definition ± Where used: ± Symbol tables ± Wide use within other algorithms A HashMap is a particular implementation of the Map interface Data Structure Building Blocks 6 ± These are implementation “building blocks” that are often used to build more-complicated data structures ± Arrays ± Linked Lists ³ Singly linked ³ Doubly linked ± Binary Trees ± Graphs ³ Adjacency matrix ³ Adjacency list
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10/22/2009 2 From interface to implementation ± Given that we want to support some interface, the designer still faces a choice ± What will be the best way to implement this interface for my expected type of use? 7 ± Choice of implementation can reflect many considerations ± Major factors we think about ± Speed for typical use case ± Storage space required Array Implementation of Stack 8 class ArrayStack implements Stack { private Object[] array; //Array that holds the Stack private int index = 0; //First empty slot in Stack public ArrayStack(int maxSize) { Obj t[ Si]} max-1 3 4 index { array = new Object[maxSize]; } public void push(Object x) { array[index++] = x; } public Object pop() { return array[--index]; } public Object peek() { return array[index-1]; } public boolean isEmpty() { return index == 0; }
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/08/2010 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell University (Engineering School).

Page1 / 6

L16cs2110fa09-6up - Abstract Data Types(ADTs 2 A method for achieving abstraction for data structures and algorithms ADT = model operations

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online