1 / 19

Lecture 23

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.

shad-pace
Download Presentation

Lecture 23

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 23 Structures and Abstract Data Types 12/3/02

  2. Information • A computer manipulates information. • How information is organized in a computer. • How information can be manipulated. • How information can be utilized.

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

  4. ADT of Rational Number • A rational number has two parts : numerator and denominator – both are integers. • Interface functions: makerational addrat multrat equalrat printrat reduce

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

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

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

  8. 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; }

  9. 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; }

  10. 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; }

  11. 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); }

  12. ADT: Rational NumberAlternate Concrete implementation II typedef struct { int ar[2]; }RATIONAL;

  13. 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]; }

  14. 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)

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

  16. 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);

  17. 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) ;

  18. 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 : ?

  19. 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 ?

More Related