Week 06 (B)


Week 06 (B) 1/21

In This Lecture ...

Coming Up ...


Data Structures, Data Types2/21

Over the last few weeks, we have

What we were actually doing is defining new data types.

Now we look in more detail about what a "data type" is ...


Abstract Data Types3/21

A data type is ...

Abstract data types (ADTs) are ...


... Abstract Data Types4/21

ADT interface provides

ADT implementation gives Note: C does not enable us to completely hide the implementation.


... Abstract Data Types5/21

Example: the stdio.h library in C

User view:

typedef ... FILE;

FILE *fopen(char *name, char *mode);
int fscanf(FILE *inf, char *format, ...);
int sscanf(char *inf, char *format, ...);
char *fgets(char *buf, int bufsize, FILE *inf);
int fgetc(FILE *inf);
int getchar();
int fprintf(FILE *outf, char *format, ...);
int printf(char *format, ...);
int fputc(int ch, FILE *outf);
int putchar(in ch);
etc. etc. etc. etc. etc.


... Abstract Data Types6/21

Most of the ADTs we deal with ...

Collections may be categorised by ... Structure/usage is generic; may be applied to many kinds of item
E.g. stacks of ints, stacks of chars, stacks of lists, stacks of trees, ...


Example: Point Data Type7/21

Point.h gives an interface

// representation of Point values
typedef struct { float x; float y; } Point;

// returns distance between two Points
float distance(Point, Point);

// moves a Point by dx,dy
void move(Point *, float, float);

Client view:   Point + struct,   distance(),   move()


... Example: Point Data Type8/21

Point.c gives implementation

#include "Point.h"

float distance(Point a, Point b) {
   float dx = a.x - b.x;
   float dy = a.y - b.y;
   return sqrt(dx*dx + dy*dy);
}
void move(Point *a, float dx, float dy) {
   a->x += dx;
   a->y += dy;
}


... Example: Point Data Type9/21

usePoint.c is a user/client

#include "Point.h"

int main(int argc, char *argv[]) {
	Point r, s;
	r.x = 5; r.y = 5;
	s.x = 4; r.y = 6;
	printf("Point r = (%0.1f,%0.1f)\n", r.x, r.y);

	float d = distance(r,s);
	printf("1st distance: %0.1f\n", d);

	move(&r, -1, +2);
	d = distance(r,s);
	printf("2nd distance: %0.1f\n", d);
	return 0;
}


... Example: Point Data Type10/21

Point is not an ADT because representation is known

If we changed representation to polar, e.g.

typedef struct { d float; theta float; } Point;

we would need to change the client (which assumes e.g. p.x)

ADTs allow implementer to change implementation without clients needing any change (except recompilation).

This is an extremely useful software engineering property.


Exercise: Making Point Less Transparent11/21

The Point implementation above requires clients to directly manipulate Point data, e.g.

Point r;  r.x = 5; r.y = 5;

Suggest additional operations so that we would not need to use Point's internal structure.

Provide implementations for all such operations.

Show how the main function above could be redone using these new operations.


Example: Point ADT12/21

PointADT.h gives interface

typedef struct PointRep *Point;

// creates a new point, initally at (x,y)
Point new(float, float);

// returns distance between two Points
float distance(Point, Point);

// moves a Point by dx,dy
void move(Point, float, float);

// destroys a Point
void delete(Point);

Client view:   Point,   new(),   distance(),   move(),   delete()


... Example: Point ADT13/21

PointADT.c gives implementation


#include "PointADT.h"

struct PointRep { float x; float y; };

Point new(float xpos, float ypos) {
   Point p = (Point)malloc(sizeof (struct PointRep));
   assert(p != NULL);
   p->x = xpos;  p->y = ypos;
   return p;
}
float distance(Point a, Point b) {
   float dx = a->x - b->x;
   float dy = a->y - b->y;
   return sqrt(dx*dx + dy*dy);
}
void move(Point a, float dx, float dy) {
   a->x += dx;
   a->y += dy;
}
void delete(Point a) {
   free(a);
}


... Example: Point ADT14/21

usePoint.c is a client

#include "PointADT.h"

int main(int argc, char *argv[]) {
	Point r, s;  float d;
	r = new(5,5);
	s = new(4,6);
	d = distance(r,s);
	printf("1st distance: %0.1f\n", d);
	move(r, -1, +2);
	d = distance(r,s);
	printf("2nd distance: %0.1f\n", d);
	delete(r);  // avoid memory leaks
	delete(s);
	return 0;
}


... Example: Point ADT15/21

Comparison of PointDT and PointADT


Structured ADTs16/21

As noted above: structured types hold a collection of items.

Typical operations on such collections:


... Structured ADTs17/21

Interface of a typical structured ADT:

typedef StackRep *Stack;

Stack newStack();
void  freeStack(Stack s);
void  push(Stack s, Item it);
Item  pop(Stack s);
int   height(Stack s);

Clients see only a pointer; representation is hidden.


... Structured ADTs18/21

Implementation of a typical structured ADT:

// what a single item looks like
typedef struct stackNode {
	Item value;
	struct stackNode *next;
} StackNode;

// representation of Stack
typedef struct {
	StackNode *top;
	int height;
} StackRep;

// implementation of functions ...


... Structured ADTs19/21

Note that in a similar way to Stack being opaque

This gives scope for making different kinds of Stacks If we define   typedef void *Item; ...


Tips for Next Week's Lab20/21


Linked lists of strings and chars


... Tips for Next Week's Lab21/21


prompt$ ./rotate -r orangepurplepink
korangepurplepin
prompt$ ./rotate -l orangepurplepink
rangepurplepinko


Produced: 24 Aug 2016