Week 06 (B)
Week 06 (B) | 1/21 |
Data Structures, Data Types | 2/21 |
Over the last few weeks, we have
Now we look in more detail about what a "data type" is ...
Abstract Data Types | 3/21 |
A data type is ...
... Abstract Data Types | 4/21 |
ADT interface provides
FILE*
... Abstract Data Types | 5/21 |
Example: the stdio.h
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 Types | 6/21 |
Most of the ADTs we deal with ...
Example: Point Data Type | 7/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 Type | 8/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 Type | 9/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 Type | 10/21 |
Point
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 | 11/21 |
The Point
Point
Point r; r.x = 5; r.y = 5;
Suggest additional operations so that we would not
need to use Point
Provide implementations for all such operations.
Show how the main
Example: Point ADT | 12/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 ADT | 13/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 ADT | 14/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 ADT | 15/21 |
Comparison of PointDT
PointADT
Structured ADTs | 16/21 |
As noted above: structured types hold a collection of items.
Typical operations on such collections:
... Structured ADTs | 17/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 ADTs | 18/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 ADTs | 19/21 |
Note that in a similar way to Stack
Item
Stack
StackOfInts
StackofStrings
typedef void *Item;
Stacks
Tips for Next Week's Lab | 20/21 |
Linked lists of strings and char
typedef struct _Node { char *data; // a pointer to string data struct _Node *next; } NodeT;
makeNode(), insertHead(), insertTail(), printList(), freeList()
main()
insertHead()
insertTail()
... Tips for Next Week's Lab | 21/21 |
prompt$ ./rotate -r orangepurplepink korangepurplepin prompt$ ./rotate -l orangepurplepink rangepurplepinko
char
rightRotateList()
leftRotateList()
main()
rightRotateList()
leftRotateList()
Produced: 24 Aug 2016