Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Data Types.

Similar presentations


Presentation on theme: "Abstract Data Types."— Presentation transcript:

1 Abstract Data Types

2 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 data objects that share a defined set of properties and operations for processing the objects. That collection and those operations form a concept that may be implemented using a particular hardware or software data structure.

3 ADT Some programming language provide explicit mechanism to support the distinction between specification and implementation. C++, Java : a class C does not have an explicit mechanism for implementing ADTs, but it is still possible and desirable to design your data types using the same notion. Example : lists, sets, graphs along with their operations

4 Why Abstract Data Types?
User of the ADT “sees” only the interface to the objects; the implementation details are “hidden” in the definition of the ADT The user is constrained to manipulate the object solely through the functions (operations) that are provided. The designer may still alter the representation as long as the new implementations of the operations do not change the user interface. This means that users will not have to recode their algorithms.

5 ADT 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 Examples of ADTs String ADT
definition of string object (alphabet, sequence) definition of string operations Boolean StringCopy (srcString, dstString) Boolean StringConcat (srcString, dstString) Boolean StringCompare (SrcString, dstString) Void StringPrint (String); possible implementations array implementation linked list implementation circular doubly-linked list implementation

7 ADT Example: Rational Numbers
Can be written as a/b where a and b are int and b != 0 Precision may be lost in representing as a single number A rational number has two parts : numerator and denominator – both are integers. Interface functions makerational add mul equal printrat reduce

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

9 Several Ways to Represent Rational
typedef struct { int numerator; int denominator; } Rational; int ar[2];

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

11 ADT: Rational Number implementation 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; }

12 ADT: Rational Number implementation 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; }

13 ADT: Rational Number Concrete implementation I
RATIONAL mult (RATIONAL r1, RATIONAL 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); }

14 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; } 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];

15 The List ADT LIST makenull () : returns an empty list
A list : <A1, A2, ... , AN> of size N. Special list of size 0 : an empty list Operations: LIST makenull () : returns an empty list LIST makelist (ETYPE) : makes a list containing a single element void printlist (LIST) int search(ETYPE, LIST) : searches whether a key is in the list void insert (ETYPE, LIST) void delete (ETYPE, LIST) ETYPE findKth (LIST)

16 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 * )

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

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

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

20 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 ?

21 Concrete implementation of SET ADT
typedef struct node { ETYPE elem; struct node * next; } * SET; Implementation 3, 4 : linked list Sorted or unsorted adjoin : member : delete : union , intersection

22 Another ADT Example: Stacks
LIFO: Last-In, First-Out Like the stack of trays at the cafeteria “Push” a tray onto the stack “Pop” a tray off the stack Useful in many contexts

23 The Stack ADT initialize push pop empty full Operations Objects:
a finite sequence of elements of the same type additions restricted to one end of the sequence called the top of the stack deletions also restricted to the same end Operations initialize push pop empty full

24 Stack Properties and Attributes
LIFO data structures. All accesses are done to the element referenced by top. The top always refers to the topmost element in the stack. Insertions are done “above” the top element. Attributes size : The number of elements in the stack: size >= 0 at all times. top : The topmost element of the stack, refers to null, a special value indicating top doesn’t reference anything in the stack. 6-24

25 Example 3 :: Last-In-First-Out STACK
Assume:: stack contains integer elements void init (stack *s); /* Create a new stack */ void push (stack *s, int element); /* Insert an element in the stack */ int pop (stack *s); /* Remove and return the top element */ int isempty (stack *s); /* Check if stack is empty */ int isfull (stack *s); /* Check if stack is full */ Spring 2012 Programming and Data Structure

26 Programming and Data Structure
Contd. We shall look into two different ways of implementing stack: Using arrays Using linked list Spring 2012 Programming and Data Structure

27 Example:: First-In-First-Out QUEUE
Assume:: queue contains integer elements void enqueue (queue *q, int element); /* Insert an element in the queue */ int dequeue (queue *q); /* Remove an element from the queue */ queue *create(); /* Create a new queue */ int isempty (queue *q); /* Check if queue is empty */ int size (queue *q); /* Return the no. of elements in queue */ Spring 2012 Programming and Data Structure

28 Stack Implementations: Using Array
Spring 2012 Programming and Data Structure

29 Programming and Data Structure
STACK USING ARRAY PUSH top top Spring 2012 Programming and Data Structure

30 Programming and Data Structure
STACK USING ARRAY POP top top Spring 2012 Programming and Data Structure

31 stack.h #define SIZE 200 #define ERROR 1 #define OK 0 typedef struct { int data[SIZE]; int tos; } stack ; void init(stack *) ; int push(stack * , int) ; int pop(stack *) ; int top(stack *, int *) ; int isEmpty(stack *) ; int isFull(stack *) ;

32 stack.c #include "stack.h" void init(stack *s) { s->tos = -1; } int push(stack *s, int n) { if(isFull(s)) { printf("The STACK is full\n"); return ERROR ; s->tos++; s->data[s->tos]=n; return OK ; int Pop(stack *s) { if(isEmpty(s)) { printf("The STACK is empty\n"); return ERROR ; } s -> tos-- ; return OK ; int isFull(stack *s) { return s->tos == SIZE-1; int isEmpty(stack *s) { return s->tos == -1;

33 stack.c int Top(stack *s , int *val) { if (isEmpty(s)) { printf("The STACK is empty\n") ; return ERROR ; } *val = (s -> data[s -> tos]) ; return OK ;

34 Array Implementation of Stack
CAPACITY too large: waste memory CAPACITY too small: wasted space data data

35 Compiling the datatype
$ gcc -c stack.c We get the object module stack.o. We can construct library from it.

36 User Program: testStack.c
#include <stdio.h> #include "stack.h" int main() // testStack.c { stack s ; int x , err , val ; char c ; init(&s); printf(" ’U’ for push (U 15)\n ’O’ for pop\n ’T’ for top printf(" ’E’ for exit :\n");

37 while((c = getchar()). = ’e’ && c
while((c = getchar()) != ’e’ && c != ’E’) switch(c) { case ’U’ : scanf("%d",&x); err = push(&s,x); break; case ’O’ : err = pop(&s); case ’T’ : err = top(&s , &val) ; if(!err) printf("%d\n", val); case ’\n’ : case ’\t’ : case ’ ’ : break; default : printf("Token Unknown\n"); } return 0;

38 Compiling the user program
$ gcc -Wall testStack.c stack.o We get the executable module a.out.

39 Stack: Linked List Structure
PUSH OPERATION top Spring 2012 Programming and Data Structure

40 Stack: Linked List Structure
POP OPERATION top Spring 2012 Programming and Data Structure

41 Representation struct stacknode { int data ; struct stacknode *next ; }; typedef struct stacknode *stack ;

42 Interface functions (stackl.h)
#include <stdio.h> #include <stdlib.h> #define ERROR 1 #define OK 0 struct stacknode { int data ; struct stacknode *next ; } ; typedef struct stacknode, *stack ; #define INIT(s) ((s)=NULL) #define ISEMPTY(s) ((s) == NULL) int push(stack * , int) ; int pop(stack *) ; int top(stack, int *) ;

43 Implementation File: stackSR.c
#include "stackSR.h" int push(stack *s, int n) { // stackSR.c stack temp ; temp=(stack)malloc(sizeof(node)) ; if(temp == NULL) { printf("The STACK is full\n"); return ERROR ; } temp->data=n; temp->next=*s ; *s = temp ; return OK ;

44 stackSR.c continued int pop(stack *s) { stack temp ; if(ISEMPTY(*s)) { printf("The STACK is empty\n"); return ERROR ; } temp=*s; *s=(*s)->next ; free(temp) ; return OK ;

45 stackSR.c continued int top(stack s , int *val) { if(ISEMPTY(s)) { printf("The STACK is empty\n") ; return ERROR ; } *val = s -> data ; return OK ;

46 User Program: testStack.c
#include <stdio.h> #include "stackSR.h" int main() // testStackSR.c { stack s ; int x , err , val ; char c ; INIT(s); printf(" ’U’ for push (U 15)\n ’O’ for pop\n ’T’ for top printf(" ’E’ for exit :\n");

47 while((c = getchar()). = ’e’ && c
while((c = getchar()) != ’e’ && c != ’E’) switch(c) { case ’U’ : scanf("%d",&x); err = push(&s,x); break; case ’O’ : err = pop(&s); case ’T’ : err = top(s , &val) ; if(!err) printf("%d\n", val); case ’\n’ :case ’\t’ :case ’ ’ : break; default :printf("Token Unknown\n"); } return 0;

48 Programming and Data Structure
Queue Implementation Spring 2012 Programming and Data Structure

49 Queues in Our Life A queue is a FIFO structure: Fast In First Out
Figure 5-1 Queues in Our Life A queue is a FIFO structure: Fast In First Out

50 Circular Arrays 1 3 2 4 5 6 7 Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end of the array “wraps around” to the start of the array

51 Queue will overgrow the array
Figure 5-16 Queue will overgrow the array Should we use VERY L A R G E ARRAYS?

52 Array implementation of queues
queueAry maxsize count front rear 7 4 1 5 front rear 11 37 22 15 3 -7 1

53 Queue on Circular Array: Representation
#define MAX 200 typedef struct { int data[MAX] ; int front , rear ; } queue; The queue may contain MAX - 1 data. #define MAX 200 typedef struct { int data[MAX] ; int front , rear, count; } queue2; The queue may contain MAX data.

54 Queue on Circular Array: Operations
void init(queue *) ; int add(queue *, int) ; int delete(queue *); int front(queue *, int *) ; int isEmpty(queue *) ; int isFull(queue *) ;

55 Interface File: queue.h
#include <stdio.h> #define MAX 200 #define ERROR 1 #define OK 0 typedef struct { int data[MAX]; int front, rear; } queue; /* Queue may contain MAX-1 data.*/ void init(queue *); int add(queue *, int); int delete(queue *); int front(queue *, int *); int isEmpty(queue *); int isFull(queue *);

56 Implementation File: queue.c
#include "queue.h" void init(queue *q) { q->front=q->rear=0; } int isEmpty(queue *q) { return q->rear == q->front; } int isFull(queue *q) { return (q->rear+1)%MAX == q->front; }

57 int add(queue *q, int n) { if (isFull(q)) return ERROR; q->data[q->rear]=n; q->rear=(q->rear+1)%MAX; return OK ; } int delete(queue *q) { if (isEmpty(q)) return ERROR ; q->front=(q->front+1)%MAX ;

58 int front(queue. q , int. v) { if (isEmpty(q)) return ERROR ;
int front(queue *q , int *v) { if (isEmpty(q)) return ERROR ; *v=q->data[(q->front)%MAX] ; return OK ; }

59 User Program: testQueue.c
#include "queue.h" int main() { queue q ; int x , err , val ; char c; init(&q); printf(" ’A’ for add (A 15)\n") ; printf(" ’D’ for delete\n ’F’ for front\n ’E’ for exit while((c = getchar()) != ’e’ && c != ’E’) switch(c) {

60 case ’A’ : scanf("%d",&x); err = add(&q,x); if(err) printf("Queue is full\n") ; break; case ’D’ : err = delete(&q); printf("Q empty\n") ; case ’F’ : err = front(&q , &val)

61 if(err) printf("Q empty\n") ; else printf("%d\n",val); break; case ’\n’ :case ’\t’ :case ’ ’ : default : printf("Token Unknown\n"); } return 0 ;

62 END


Download ppt "Abstract Data Types."

Similar presentations


Ads by Google