110 likes | 270 Views
Definition of a structured set. Definition (structured set) A structured set is a set in the mathematical sense, where the elements satisfy some relations between them. Short:. structured set Sr = set S + relation R.
E N D
Definition of a structured set Definition (structured set) A structured set is a set in the mathematical sense, where the elements satisfy some relations between them. Short: structured set Sr = set S + relation R
Graphical representation of a set, and linear, tree and graph structures
Definition of an ADT Object Definition (ADT Object) In the context of abstract data type (ADT), we define an ADT objectas a structured set of elements (i.e. a set where the elements satisfy some relation R between them), supplied with operations acting on these elements and which must ensure that the relation R is guaranteed. Short: ADT object = set S + relation R + Operations O
Operations Constructor Functions • create(&o, c) • destroy(&o) Manipulator Functions • put(o,&e) : adds element *e in P order to o. • get(o,&e) • new_capacity(o,c) Access Functions • consult(o,&e) : copies the element in P order from o to *e. • is_empty(o) • is_full(o) • … Traverse Function • traverse(o, f, &b) : calls the user supplied function f for all elements of o, using the buffer b.
Data Type Specification For an array implementation typedef struct obj_descr_t { elt_t *base; //points to element base[0] of //the ADT object array instance ... //all what is needed for the impl. of the //ADT object fcts } obj_descr_t;
Data Type Specification For a linked list implementation typedefstructp_node_t { //p stands for protocol P //of the ADT oject elt_t: e; //element to be stored in a node ... //all what is needed for the impl. of the ADT //object protocol P } p_node_t; typedefstructobj_descr_t { p_node_t*root; //points to the entry element of the ADT object instance ... //all what is needed ... } obj_descr_t;
stack_create() (c3) s (c4) (m3) s (m1) sd elt a elt a (m2) (c1) sd->top (c2') (c2) typedef charelt_t; typedef void *stack_t; stack_t *stack_create(void) { (c1) stack_descr_t *sd; (c2) sd = malloc(sizeof *sd); (c3) sd->top = NULL; (c4) return sd; } typedef struct stack_node_t { elt_t e; struct stack_node_t *previous; } stack_node_t; typedef struct stack_descr_t { stack_node_t *top; } stack_descr_t; main() { (m1) stack_t s; (m2) elt_t elt = 'a'; (m3) s = stack_create(); (m4) stack_put(s,&elt); } Imperative & System Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 9, 13 November 2013
stack_put(s,&e) (p1) (p1) s e (p5) (p2') (p1) s–>top s (p3) s a a (p4) elt elt a a (p2'') new_node (p2) (m4) stack_put(s,&elt); (p1) void stack_put(stack_t s, elt_t *e) { (p2) stack_node_t *new_node = malloc(sizeof(stack_node_t)); (p3) new_node->e = *e; (p4) new_node->previous = s->top; (p5) s->top = new_node; } typedef charelt_t; typedef void *stack_t; typedef struct stack_node_t { elt_t e; struct stack_node_t *previous; } stack_node_t; typedef struct stack_descr_t { stack_node_t *top; } stack_descr_t; Beware, there is a little error in 'stack_put' : the generic pointer 's' can't be dereferenced without casting: ((stack_descr_t *) s) –> top