adt stack implementazione in linguaggio c - diee.unica.itarmano/lt1/pdf/lt1-7c.pdf · 3 3 specifica...
TRANSCRIPT
3
3
Stack
Specifica Algebrica dell’ADTSTACK
type Stack [Item] uses Item, Booleansyntax
NEWSTACK : → Stack
PUSH : Item x Stack → StackISEMPTY : Stack → BooleanPOP : Stack → Stack
TOP : Stack → ItemREPLACE : Item x Stack → Stack
semantics...
end Stack.
4
4
Stack
Specifica Algebrica dell’ADTSTACKsemanticsfor all stk in Stack; el, elm in Item;ISEMPTY(NEWSTACK) = true
ISEMPTY(PUSH(elm,stk)) = falsePOP(NEWSTACK) = errorPOP(PUSH(elm, stk)) = stk
TOP(NEWSTACK) = errorTOP(PUSH(elm, stk)) = elmREPLACE(elm,NEWSTACK) = errorREPLACE(el,PUSH(elm, stk)) = PUSH(el,stk)
6
6
Stack
STACK: PUSH
Vista Grafica
STACK push(Item , Stack ) ;Interfaccia C
PUSH : Item x Stack → StackSintassi
7
7
Stack
STACK: ISEMPTY
Vista Grafica
Boolean isempty(Stack ) ;Interfaccia C
ISEMPTY : Stack → BooleanSintassi
False True
9
9
Stack
STACK: TOP
Vista Grafica
STACK top(Stack ) ;Interfaccia C
TOP : Stack → ItemSintassi
value
10
10
Stack
STACK: REPLACE
Vista Grafica
STACK replace(Item , Stack ) ;Interfaccia C
REPLACE : Item x Stack → StackSintassi
1
2
11
11
Stack
Stack – Interfacce C (file stack.h)#ifndef __STACK
#define __STACK
#include "utils.h"
STACK newSTACK() ; // instance creation (for dynamic allocation)
STACK push(Item, STACK) ; // push an Item into a STACK
STACK replace(Item, STACK) ; // replace the top of a STACK
Boolean isEmpty(STACK) ; // check whether the STACK is empty or not
Item top(STACK) ; // return the top of a STACK
STACK pop(STACK) ; // pop the top from a STACK
STACK initSTACK(STACK) ; // instance initialization (for static alloc.)
void deleteSTACK(STACK) ; // instance deletion
void displaySTACK(char *, STACK) ; // display the contents of a STACK
#endif
13
13
Stack
…
Stack – Implementazione Statica
struct stack{
int index ; Item buffer[MAXSTK];
} ;} index=i
MAXSTK-1
0
1
…
15
15
Stack
Stack – Implementazione statica#ifndef __STATIC_STACK#define __STATIC_STACK
/* TYPES AND CONSTANTS */
#define MAXSTK 100#include "stack_item.h“
/* STATIC STACK: STRUCTURE */
struct stack {int index ; /* current top */Item buffer[MAXSTK] ; /* data storage */
} ;typedef struct stack * STACK ;
#include “stack.h“ /* STACK INTERFACE */#endif
16
16
Stack
Stack – Implementazione dinewSTACKSTACK newSTACK() { // no need for instance creation, here }
STACK initSTACK(STACK S) {S->index = -1 ; return S ;
}
MAXSTK-1
0
1
…
index = -1
17
17
Stack
i2
Stack – Implementazione di pushSTACK push(Item item, STACK S) {if ( (S->index) >= MAXSTK - 1 )
ERROR("Stack is full!");else{S->index)++ ; S->buffer[S->index] = item ;
}return S ;
}
index=i
MAXSTK-1
i1
…0
…
index=i+1
MAXSTK-1
…0
…push(Itemi2, STACK s)
18
18
Stack
Stack – Implementazione diisEmptyBoolean isEmpty(STACK S) {ALERT("Executing isEmpty");return (Boolean) ( S->index < 0 );
}
MAXSTK-1
0
1
…
index = -1
index≠0
MAXSTK-1
i1
…0
…
False True
19
19
Stack
Stack – Implementazione di topItem top(STACK S) {ALERT("Executing top: index = %d",S->index);if ( S->index < 0 ) {ERROR("Stack is empty!"); return 0.0;
}else return S->buffer[S->index] ;}
index=i
MAXSTK-1
i1
…0
…top(STACK s) i1
20
20
Stack
Stack – Implementazione di popSTACK pop(STACK S) {ALERT("Executing pop: index = %d",S->index);if ( S->index < 0 )
ERROR("Stack is empty!");else S->index-- ;return S ;
}
index=i
MAXSTK-1
i1
…0
…
pop(STACK s)
MAXSTK-1
0
…
i3index=i-1
21
21
Stack
Stack – Implementazione direplaceSTACK replace(Item item, STACK S) {if ( S->index < 0 )
ERROR("Stack is empty!“);else S->buffer[S->index] = item ;return S ;
}
index=i
MAXSTK-1
i1
…0
…replace(Item i2, STACK s)
index=i
MAXSTK-1
i2
…0
…
22
22
Stack
Stack – Verifica Semantica delle Primitive#include <stdio.h>#include "auto_stack.h"/* STACK TESTING */// automatic allocation for STACK instances// no need for instance creation and deletion#include "test_stack_suite_1.c“
int main() {Item item ;struct stack myStack ;STACK s ;s = initSTACK(&myStack) ;// instead of NewSTACKtest_suite_1(s) ;
return 0 ;
}
23
23
Stack
Stack – Verifica Semantica delle Primitivevoid test_suite_1(STACK s) {
int i ; Item item ;/* isEmpty(newStack) = true */printf("isEmpty(newStack) = true ? ");(isEmpty(s)) ? printf("OK\n") : printf("NO\n");/* isEmpty(push(anItem,S)) = false */printf("isEmpty(push(anItem,S)) = false ? ");push( 0.0, s);(!isEmpty(s)) ? printf("OK\n") : printf("NO\n");/* pop(push(anItem,S)) = S */ printf("pop(push(anItem,S)) = S ? ") ;push(1.0, s); push((2.0, s);pop(s) ;(top(s) == 1.0) ? printf("OK\n") : printf("NO\n");
}
25
25
Stack
Stack – ImplementazioneDinamicatypedef struct node {
Item item ;struct node * next ;
} Node ; } item
next
typedef typedef Node * STACK ; item
next
item
next
item
next
26
26
Stack
Stack – Implementazionedinamica
#ifndef __DYN_STACK#define __DYN_STACK
/* DYNAMIC STACK: STRUCTURE */#include "stack_item.h"
typedef struct node {Item item ;struct node * next ;
} Node ;
typedef Node * STACK ;
/* --- STACK INTERFACE --- */#include "stack.h"
#endif
28
28
Stack
Stack – Implementazione di pushSTACK push(Item item, STACK S){Node * ptr ;ptr = (Node *) malloc(sizeof(struct node)) ;if ( ptr == NULL ) goto push_exception;ptr->item = item ; ptr->next = S ;return (STACK) ptr ;
push_exception:exit(-1) ; // unrecoverable error
}
i1
next
next
push(Itemi2, STACK s) i1
next
next
i2
next
29
29
Stack
Stack – Implementazione diisEmptyBoolean isEmpty(STACK S){return (Boolean) ( S == NULL ) ;
}
False
i1
next
next
i2
next
30
30
Stack
Stack – Implementazione di topItem top(STACK S) {if ( S == NULL ) goto top_exception ;return S->item ;
top_exception:ERROR("Stack is Empty!") ;return theDummyItem ;
}
top(STACK s) i1i1
next
next
31
31
Stack
Stack – Implementazione di popSTACK pop(STACK S) {Node * next ;if ( S == NULL ) goto pop_exception ;next = S->next ; free(S) ;return next ;
pop_exception:ERROR("Stack is Empty!") ;return S ;
}
pop(STACK s)i1
next
next
i2
next i1
next
next
32
32
Stack
Stack – Implementazione direplaceSTACK replace(Item item, STACK S) {if ( S == NULL ) goto replace_exception ;S->item = item ;return S;
replace_exception:ERROR("Stack is Empty!") ;return S ;
}
replace(Item i2, STACK s)
i1
next
next
i2
next
next
33
33
Stack
Stack – Verifica Semantica delle Primitive#include <stdio.h>#include "dyn_stack.h"#include "test_stack_suite_1.c"
/* DYNAMIC STACK: TESTING */int main(int argc, char ** argv) {int i ; Item item ;STACK s = newSTACK() ;test_suite_1(s) ;deleteSTACK(s) ;return 0 ;
}/* end main */
34
34
Stack
Stack – Verifica Semantica delle Primitive/* STACK TESTING: TEST SUITE #1 */void test_suite_1(STACK s) {int i ; Item item ;/* isEmpty(newStack) = true */printf("isEmpty(newStack) = true ? ") ;( isEmpty(s) ) ? printf("OK\n") : printf("NO\n") ;/* isEmpty(push(anItem,S)) = false */printf("isEmpty(push(anItem,S)) = false ? ") ;s = push((double) 0, s) ;( ! isEmpty(s) ) ? printf("OK\n") : printf("NO\n") ;/* pop(newStack) = error */s = pop(s) ;printf("pop(newStack) = error ? ") ;s = pop(s) ;/* pop(push(anItem,S)) = S */printf("pop(push(anItem,S)) = S ? ") ;…
35
35
Stack
Stack – Verifica Semantica delle Primitive
…s = push((double) 1, s) ;s = push((double) 2, s) ; s = pop(s) ;( top(s) == 1.0 ) ? printf("OK\n") : printf("NO\n");/* top(newStack) = error */s = pop(s) ;printf("top(newStack) = error ? ") ;item = top(s) ;/* top(push(anItem,S)) = anItem */printf("top(push(anItem,S)) = anItem ? ") ;s = push((double) 11, s) ;s = push((double) 22, s);( top(s) == 22.0 ) ? printf("OK\n"): printf("NO\n");…
36
36
Stack
Stack – Verifica Semantica delle Primitive
…/*replace(anItem1,push(anItem,S))=push(anItem1,S)*/printf("replace(anItem1,push(anItem,S)) =
push(anItem1,S) ? ") ;s = push((double) 1, s) ;s = push((double) 2, s) ;replace((double) 3, s) ; item = top(s) ;s = pop(s);( ( top(s) == 1.0 ) && ( item == 3.0 ) ) ? printf("OK\n") : printf("NO\n") ;
}/* end main */