Пример #1
0
void machine_initialize(_pmachine m)
{
    register_init(&m->_r_eax);
    register_init(&m->_r_ebx);
    register_init(&m->_r_ecx);
    register_init(&m->_r_edx);
    register_init(&m->_r_pc);
    register_init(&m->_r_flag);
    register_init(&m->_r_eip);

    
    stack_initialize(&m->stack);
    m->instructions = NULL;
    m->instruction_tail = NULL;
}
int main(void){
    int i;
    stack_t* s;
    pessoa p;
    stack_initialize(&s,constructor_pessoa,destructor_pessoa);
    for(i=0;i<5;i++){
        printf("Cadastrando pessoa %d\n",i+1);
        cadastra_pessoa(&p);
        stack_push(s,&p);
    }
    while(!stack_empty(s)){
        printf("\n**Imprimindo pessoa**\n");
        p =  *(pessoa*) stack_top(s);
        stack_pop(s);
        imprime_pessoa(&p);

        printf("\n");
    }
    stack_delete(&s);
    return 0;
}
Пример #3
0
int main(int argc, char** argv) {
	
	stack * s = stack_initialize();
	for(int i=0; i<10; i++){
		mystruct *j = (mystruct*)malloc (sizeof(mystruct));
		j->a = i;
		stack_push(s, j);										//push any user defined structure into the stack
	}
	int i=0;
	while(!stack_isempty(s)){									//check whether stack empty or not
		i++;
		printf("%d\n", ((mystruct *)stack_top(s))->a);
		stack_pop(s);											//pop from stack
	}
	for(int i=0; i<10; i++){
		int *j = (int*)malloc (sizeof(int));
		*j = i;
		stack_push(s, j);
	}
	mystruct *j = (mystruct*) malloc(sizeof(mystruct));
	stack_push(s,j);											//Heterogenous stack
	stack_destroy(s);											//Destroy the stack
	return 0;
}
Пример #4
0
int main(void)
{
    stack_t *stack;
    int getf,buffer[128],errcode;
    size_t counter,size;
    stack = stack_initialize(sizeof(int),DEFAULT_RELEASE_FUNCTION);
    if(!stack){
        fputs("Error!\n",stderr);
        return -1;
    }
    while(1){
        ignore_space(stdin);
        getf = fgetc(stdin);
        if(getf == 's'){
            fputs("set ",stdout);
            counter = 0;
            while(counter != 128){
                ignore_space(stdin);
                getf = fgetc(stdin);
                ungetc(getf,stdin);
                if(getf == '\n'
                    || (getf != '+' && getf != '-' && !isdigit(getf))){
                    break;
                }
                buffer[counter] = read_integer(stdin);
                fprintf(stdout,"%d ",buffer[counter]);
                counter++;
            }
            ignore_to_newline(stdin);
            errcode = stack_push_many_elements(stack,counter,buffer);
            if(errcode){
                fputs("Error!\n",stderr);
                return -1;
            }
            fputc('\n',stdout);
            fputs("stack trace : ",stdout);
            stack_trace(stack);
        }
        else if(getf == 'g'){
            fputs("get ",stdout);
            ignore_space(stdin);
            size = read_integer(stdin);
            ignore_to_newline(stdin);
            errcode = stack_pop_many_elements(stack,size,buffer);
            if(errcode){
                fputs("Error!\n",stderr);
                return -1;
            }
            counter = 0;
            while(counter != size){
                fprintf(stdout,"%d ",buffer[counter]);
                counter++;
            }
            fputc('\n',stdout);
            fputs("stack trace : ",stdout);
            stack_trace(stack);
        }
        else if(!isspace(getf)){
            break;
        }
    }
    stack_release(stack);
    return 0;
}