Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 261 - Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.

Similar presentations


Presentation on theme: "CS 261 - Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing."— Presentation transcript:

1 CS 261 - Winter 2011 Abstract Data Types

2 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

3 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

4 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

5 Metaphors ADT view are often described by metaphors (e.g., stack of plates). Easy to remember. Easy to understand.

6 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);

7 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

8 The Implementation View void pushStack (struct stack * stk, double val) { stk->data.add(val); } int stackIsEmpty (struct stack * stk) { return dyArraySize(stk->data) == 0: }

9 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

10 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

11 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

12 BAG interface Provide functions for operations, effectively hiding implementation (names may vary) addBag (container, value) testBag (container, value) removeBag (container, value) sizeBag (container, value)

13 Stack as Design Pattern Problem: Maintain collection in Last-In, First-Out format Forces: LIFO Counter-forces: non-LIFO Solution: again, abstract Stack interface

14 The Classic Implementation Techniques Arrays and Dynamic Arrays (Vectors) Linked Lists Binary Trees, Balanced Trees Heaps Hash Tables Skip Lists Etc etc etc

15 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.

16 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

17 First function - initialize void initBag (struct arrayBag * b) { }

18 Add to bag void addBag (struct arrayBag * b, EleType v) { }

19 Test for contains int testBag (struct arrayBag * b, EleType v) { }

20 Remove from bag void removeBag (struct arrayBag * b, EleType v) { }

21 Return size of collection int bagSize (struct arrayBag * b) { }

22 How about stack? void pushStack (struct arrayBag * b, EleType v) { }

23 Test for stack empty int isStackEmpty (struct arrayBag * b) { }

24 Top of stack EleType topStack (struct arrayBag * b) { }

25 Pop top element void popStack (struct arrayBag * b) { }

26 That’s all for today Hand in your worksheets, put your name on them


Download ppt "CS 261 - Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing."

Similar presentations


Ads by Google