190 likes | 282 Views
Lecture 23. Structures and Abstract Data Types 12/3/02. Information. A computer manipulates information. How information is organized in a computer. How information can be manipulated. How information can be utilized. Abstract Data Types.
E N D
Lecture 23 Structures and Abstract Data Types 12/3/02
Information • A computer manipulates information. • 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: • the implementation of these operations is written • any other part of the program that needs to perform some operation on the ADT calls the appropriate function • If implementation details change, it will be transparent to the rest of the program.
ADT Examples • Integers of arbitrary size • Operations: add, subtract, multiply, divide • Set • Operations: MakeSet, adjoin, member, union, intersection, difference, remove • Complex number • Operations: add, subtract, multiply, divide • Rational number: • Operations: reduce, add, subtract, multiply, divide
ADT: Rational NumberInterface 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 NumberConcrete implementation I RATIONAL makerational (int x, int y) { RATIONAL r; r.numerator = x; r.denominator = y; return r; } typedef struct { int numerator; int denominator; }RATIONAL; RATIONAL reduce (RATIONAL r) { int g; g = gcd (r.numerator,r.denominator); r.numerator /= g; r.denominator /= g; return r; } int numerator (RATIONAL r) { return r.numerator; } int denominator (RATIONAL r) { return r.denominator; }
ADT: Rational Numberimplementation of add (1) typedef struct { int numerator; int denominator; } RATIONAL; RATIONAL add (RATIONAL r1, RATIONAL r2) { RATIONAL r; int g; g = gcd(r1.denominator,r2.denominator); r.denominator = lcm(r1.denominator,r2.denominator); r.numerator = r1.denominator*r2.denominator/g; r.numerator += r2.denominator*r1.numerator/g; return r; }
ADT: Rational Numberimplementation of add (2) typedef struct { int numerator; int denominator; }RATIONAL; RATIONAL add (RATIONAL r1, RATIONAL r2) { RATIONAL r; r.numerator = r1.numerator*r2.denominator +r2.numerator*r1.denominator; r.denominator=r1.denominator*r2.denominator; return r; }
ADT: Rational NumberConcrete implementation I RATIONAL mult (ARTIONAL r1, ARTIONAL r2) { RATIONAL r; r.numerator = r1.numerator*r2.numerator; r.denominator = r1.denominator*r2.denominator; r = reduce (r); return r; } typedef struct { int numerator; int denominator; }RATIONAL; int equal (RATIONAL r1, RATIONAL r2) { return (r1.numerator*r2.denominator==r2.numerator*r1.denominator); } void printrat (RATIONAL r) { printf (“%d / %d “,r.numerator, r.denominator); }
ADT: Rational NumberAlternate Concrete implementation II typedef struct { int ar[2]; }RATIONAL;
ADT: Rational NumberConcrete implementation II RATIONAL makerational (int x, int y) { RATIONAL r; r.ar[0] = x; r.ar[1] = y; return r; } typedef struct { int ar[2] ; }RATIONAL; RATIONAL reduce (RATIONAL r) { int g; g = gcd (r.numerator,r.denominator); r.a[0] /= g; r.a[1] /= g; return r; } int numerator (RATIONAL r) { return r.a[0]; } int denominator (RATIONAL r) { return r.a[1]; }
The List ADT • A list : <A1, A2, ... , AN> of size N. • Special list of size 0 : an empty list • Operations: • 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) • findKth (list)
Array Implementation of List typedef int ETYPE; typedef struct { ETYPE elements[MAXS]; int size; } LIST; LIST makenull () ; LIST makeList (ETYPE) ; void printList (LIST) ; int IsEmpty (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, SET*); void intersection (SET, 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 ?