Structures and Abstract Data Types

About This Presentation
Title:

Structures and Abstract Data Types

Description:

Lecture 23 Structures and Abstract Data Types 12/3/02 –

Number of Views:37
Avg rating:3.0/5.0
Slides: 20
Provided by: Sude2
Category:

less

Transcript and Presenter's Notes

Title: Structures and Abstract Data Types


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) RA
TIONAL mult (RATIONAL, RATIONAL) RATIONAL reduce
(RATIONAL) Equality testing int equal
(RATIONAL, RATIONAL) Print void printrat
(RATIONAL)
8
ADT Rational NumberConcrete 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
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,r
2.denominator) r.denominator
lcm(r1.denominator,r2.denominator) r.numerator
r1.denominatorr2.denominator/g r.numerator
r2.denominatorr1.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.numeratorr2.deno
minator r2.numeratorr1.denominator r.denomin
atorr1.denominatorr2.denominator return r
11
ADT Rational NumberConcrete implementation I
RATIONAL mult (ARTIONAL r1, ARTIONAL r2)
RATIONAL r r.numerator
r1.numeratorr2.numerator r.denominator
r1.denominatorr2.denominator r reduce
(r) return r
  • typedef struct
  • int numerator
  • int denominator
  • RATIONAL

int equal (RATIONAL r1, RATIONAL r2) return
(r1.numeratorr2.denominatorr2.numera
torr1.denominator)
void printrat (RATIONAL r) printf (d / d
,r.numerator, r.denominator)
12
ADT Rational NumberAlternate Concrete
implementation II
  • typedef struct
  • int ar2
  • RATIONAL

13
ADT Rational NumberConcrete implementation II
  • typedef struct
  • int ar2
  • RATIONAL

RATIONAL makerational (int x, int y) RATIONAL
r r.ar0 x r.ar1 y return r
RATIONAL reduce (RATIONAL r) int g
g gcd (r.numerator,r.denominator)
r.a0 / g r.a1 / g return
r
int numerator (RATIONAL r) return
r.a0 int denominator (RATIONAL r)
return r.a1
14
The List ADT
  • A list ltA1, A2, ... , ANgt 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 elementsMAXS
  • 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 elemMAX
  • 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 elemMAX
  • 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 ?
Write a Comment
User Comments (0)
About PowerShow.com