L16cs2110fa08-6up - Abstract Data Types (ADTs) ! A method...

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

View Full Document Right Arrow Icon
Standard ADTs Lecture 16 CS2110 – Fall 2008 2 Abstract Data Types (ADTs) ! A method for achieving abstraction for data structures and algorithms ! ADT = model + operations ! Describes what each operation does, but not how it does it ! An ADT is independent of its implementation ! In Java, an interface corresponds well to an ADT " The interface describes the operations, but says nothing at all about how they are implemented ! Example: Stack interface/ADT public interface Stack { public void push(Object x); public Object pop(); public Object peek(); public boolean isEmpty(); public void clear(); } 3 ! ADT Queue " Operations: void add(Object x); Object poll(); Object peek(); boolean isEmpty(); void clear(); ! Where used: " Simple job scheduler (e.g., print queue) " Wide use within other algorithms ! ADT PriorityQueue " Operations: void insert(Object x); Object getMax(); Object peekAtMax(); boolean isEmpty(); void clear(); ! Where used: " Job scheduler for OS " Event-driven simulation " Can be used for sorting " Wide use within other algorithms 4 Sets ! ADT Set " Operations: void insert(Object element); boolean contains(Object element); void remove(Object element); boolean isEmpty(); void clear(); ! Where used: " Wide use within other algorithms ! Note: no duplicates allowed " A “set” with duplicates is sometimes called a multiset or bag 5 Dictionaries ! 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 6 Data Structure Building Blocks ! 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
7 Array Implementation of Stack 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) { 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; } public void clear() { index = 0; } } max-1 3 2 1 0 4 index O(1) worst- case time for each operation Question: What can go wrong? 8
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 02/01/2010 for the course CS 2110 at Cornell University (Engineering School).

Page1 / 5

L16cs2110fa08-6up - Abstract Data Types (ADTs) ! A method...

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