Lecture 23 Structures and Abstract Data Types 12302
Lecture 23 Structures and Abstract Data Types 12/3/02
Information A computer manipulates information. n n n How information is organized in a computer. How information can be manipulated. How information can be utilized.
Abstract Data Types A useful tool for specifying the logical properties of a data type is the abstract data type, or ADT. A data type is a collection of values and a set of operations on those values. That collection and those operations form a mathematical concept that may be implemented using a particular hardware or software data structure.
ADT of Rational Number A rational number has two parts : numerator and denominator – both are integers. Interface functions: makerational addrat multrat equalrat printrat reduce
ADT A data structure together with its operations, without specifying an implementation. Abstract data types are mathematical abstractions Example : lists, sets, graphs along with their operations The basic idea: n the implementation of these operations is written n any other part of the program that needs to perform some operation on the ADT calls the appropriate function n If implementation details change, it will be transparent to the rest of the program.
ADT Examples Integers of arbitrary size n Operations: add, subtract, multiply, divide Set n Operations: Make. Set, adjoin, member, union, intersection, difference, remove Complex number n Operations: add, subtract, multiply, divide Rational number: n Operations: reduce, add, subtract, multiply, divide
ADT: Rational Number Interface functions Constructor function: RATIONAL makerational (int, int); Selector functions : int numerator (RATIONAL); Int denominator (RATIONAL) ; Operations: RATIONAL add (RATIONAL, RATIONAL); RATIONAL mult (RATIONAL, RATIONAL); RATIONAL reduce (RATIONAL) ; Equality testing : int equal (RATIONAL, RATIONAL); Print : void printrat (RATIONAL) ;
ADT: Rational Number Concrete implementation I typedef struct { int numerator; int denominator; }RATIONAL; RATIONAL makerational (int x, int y) { RATIONAL r; r. numerator = x; r. denominator = y; return r; } int numerator (RATIONAL r) { return r. numerator; } int denominator (RATIONAL r) { return r. denominator; } RATIONAL reduce (RATIONAL r) { int g; g = gcd (r. numerator, r. denominator); r. numerator /= g; r. denominator /= g; return r; }
ADT: Rational Number implementation of add (1) typedef struct { int numerator; int denominator; } RATIONAL; RATIONAL add (RATIONAL r 1, RATIONAL r 2) { RATIONAL r; int g; g = gcd(r 1. denominator, r 2. denominator); r. denominator = lcm(r 1. denominator, r 2. denominator); r. numerator = r 1. denominator*r 2. denominator/g; r. numerator += r 2. denominator*r 1. numerator/g; return r; }
ADT: Rational Number implementation of add (2) typedef struct { int numerator; int denominator; }RATIONAL; RATIONAL add (RATIONAL r 1, RATIONAL r 2) { RATIONAL r; r. numerator = r 1. numerator*r 2. denominator +r 2. numerator*r 1. denominator; r. denominator=r 1. denominator*r 2. denominator; return r; }
ADT: Rational Number Concrete implementation I typedef struct { int numerator; int denominator; }RATIONAL; RATIONAL mult (ARTIONAL r 1, ARTIONAL r 2) { RATIONAL r; r. numerator = r 1. numerator*r 2. numerator; r. denominator = r 1. denominator*r 2. denominator; r = reduce (r); return r; } int equal (RATIONAL r 1, RATIONAL r 2) { return (r 1. numerator*r 2. denominator==r 2. numerator*r 1. denominator); } void printrat (RATIONAL r) { printf (“%d / %d “, r. numerator, r. denominator); }
ADT: Rational Number Alternate Concrete implementation II typedef struct { int ar[2]; }RATIONAL;
ADT: Rational Number Concrete implementation II typedef struct { int ar[2] ; }RATIONAL; RATIONAL makerational (int x, int y) { RATIONAL r; r. ar[0] = x; r. ar[1] = y; return r; } int numerator (RATIONAL r) { return r. a[0]; } int denominator (RATIONAL r) { return r. a[1]; } RATIONAL reduce (RATIONAL r) { int g; g = gcd (r. numerator, r. denominator); r. a[0] /= g; r. a[1] /= g; return r; }
The List ADT A list : <A 1, A 2, . . . , AN> of size N. Special list of size 0 : an empty list Operations: n n n n makenull () : returns an empty list makelist (elem) : makes a list containing a single element printlist (list) search(elem, list) : searches whether a key is in the list insert (elem, list) delete (elem, list) find. Kth (list)
Array Implementation of List typedef int ETYPE; typedef struct { ETYPE elements[MAXS]; int size; } LIST; LIST makenull () ; LIST make. List (ETYPE) ; void print. List (LIST) ; int Is. Empty (LIST) ; int search (ETYPE, LIST) ; void delete (ETYPE, LIST * ); void insert (ETYPE, LIST * )
Complex Number ADT typedef struct { float real; float imag; } COMPLEX; COMPLEX makecomplex (float, float) ; COMPLEX addc (COMPLEX, COMPLEX); COMPLEX subc (COMPLEX, COMPLEX); COMPLEX multc (COMPLEX, COMPLEX); COMPLEX divc (COMPLEX, COMPLEX);
SET ADT Interface functions (1): SET makenullset () ; int member (ETYPE, SET) ; SET adjoin (ETYPE, SET); SET union (SET, SET) ; SET intersection (SET, SET); Void printset (SET) ; Interface functions (2): SET makenullset () ; int member (ETYPE, SET) ; void adjoin(ETYPE, SET *); void union (SET, SET*); void intersection (SET, SET*); Void printset (SET) ;
Concrete implementation of SET ADT typedef struct { ETYPE elem[MAX]; int size; } SET; Implementation 1 : sorted array adjoin : Sorted insert member : Binary search delete : ? union : merge 2 sorted arrays intersection : ?
Concrete implementation of SET ADT typedef struct { ETYPE elem[MAX]; int size; } SET; Implementation 2 : unsorted array keep the elements in the array unsorted. adjoin : Insert at the end member : Search till found or till the end delete : Go through the array sequentially until element is found, or reach the end. Then left shift the array. union , intersection ?
- Slides: 19