CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have...
-
Upload
kelly-stokes -
Category
Documents
-
view
218 -
download
0
Transcript of CS 261 - Winter 2009 Abstract Data Types. Container Classes Over the years, programmers have...
CS 261 - Winter 2009
Abstract Data Types
Container Classes
• Over the years, programmers have identified a small number of different ways of organizing collections of data.
• These container abstractions are now the fundamental heart of the study of data structures.
• Examples, Stack, Queue, Set, Map, etc
Three Levels of Abstraction
• There are at least three levels of abstraction in the study of data structures
• ADT - Abstract Data Type, language independent
• Interface - the interface in a particular library of containers
• Implementation - the various implementations in a particular library
The ADT view(Abstract Data Type)
• Every data type can be described in a way that is independent of language/library
• E.G., A Stack is a collection that has the property that an item removed is the most recently entered item.
• Properties that are true regardless of the names given to operations in library
Metaphors
• ADT view are often described by metaphors (e.g., stack of plates). Easy to remember. Easy to understand.
The Interface View
• Gives specific names to operations
struct stack;
void initStack (struct stack * stk);
void pushStack (struct stack * stk, double val);
double topStack (struct stack * stk);
void popStack (struct stack * stk);
int isEmptyStack (struct stack * stk);
Additional Information
• The interface view gives only signatures
• Must also attach meanings (LIFO properties of stack, etc)
• Can also attach expected execution times (want push and pop to be constant time).
• Both more and less informative than ADT view
The Implementation View
void pushStack (struct stack * stk, double val) {
stk->data.add(val);
}
int stackIsEmpty (struct stack * stk) {
return dyArraySize(stk->data) == 0:
}
The Study of Data Structures
• As we study data structures we move constantly between all three views
• The ADT view is constant from one language to the next
• The interface view allows you to compare implementations
• The implementation allows you to see how it is done
The Classic ADTs
• Bag, Ordered Bag - simple collections
• Stack, Queue, Deque - ordered by insertion
• Set - unique elements, fast test
• Map (Dictionary) - key/value associations
• Priority Queue - ordered by importance
BAG as Design Pattern
• Problem: Need to maintain an unordered collection of elements, without needing to know how it is organized
• Forces: Require insertion, test and removal - time of insertion is unimportant
• Counter-forces: Time of insertion IS important
• Solution: Provide abstract interface
BAG interface
• Provide functions for operations, effectively hiding implementation (names may vary)
addBag (container, value)
testBag (container, value)
removeBag (container, value)
sizeBag (container, value)
Stack as Design Pattern
• Problem: Maintain collection in Last-In, First-Out format
• Forces: LIFO
• Counter-forces: non-LIFO
• Solution: again, abstract Stack interface
The Classic Implementation Techniques
• Arrays and Dynamic Arrays (Vectors)
• Linked Lists
• Binary Trees, Balanced Trees
• Heaps
• Hash Tables
• Skip Lists
• Etc etc etc
Now, your first worksheet
• As a way to introduce C and the worksheet idea, we will make implementations of a simple BAG and STACK using an array
• We will do these together, in class - won’t always be that way
• Worksheets are handed in, and are lightly graded.
First look at C - interface file
# define EleType double# define EQ(a, b) (a == b)
struct arrayBag { EleType data[100]; int count;};
void initBag (struct arrayBag * b); … etc
First function - initialize
void initBag (struct arrayBag * b)
{
}
Add to bag
void addBag (struct arrayBag * b, EleType v)
{
}
Test for contains
int testBag (struct arrayBag * b, EleType v)
{
}
Remove from bag
void removeBag (struct arrayBag * b, EleType v)
{
}
Return size of collection
int bagSize (struct arrayBag * b)
{
}
How about stack?
void pushStack (struct arrayBag * b, EleType v)
{
}
Test for stack empty
int isStackEmpty (struct arrayBag * b)
{
}
Top of stack
EleType topStack (struct arrayBag * b)
{
}
Pop top element
void popStack (struct arrayBag * b)
{
}
That’s all for today
• Hand in your worksheets, put your name on them
• See you on thursday