L16cs2110fa08 - Standard ADTs Lecture 16 CS2110 Fall 2008...

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

View Full Document Right Arrow Icon
Standard ADTs Lecture 16 CS2110 – Fall 2008
Background image of page 1

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

View Full DocumentRight Arrow Icon
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(); }
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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 6
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?
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Linked List Implementation of Stack class ListStack implements Stack { private Node head = null; //Head of list that //holds the Stack
Background image of page 8
Image of page 9
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 / 29

L16cs2110fa08 - Standard ADTs Lecture 16 CS2110 Fall 2008...

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

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