static char * test_create() { interpreter = interpreter_create(); mu_assert(interpreter != NULL, "failed to create interpreter"); mu_assert(stack_count(interpreter->call_stack) == 0, "Should be 0 items pon stack"); mu_assert(stack_count(interpreter->scopes) == 1, "Should only be 1 stack frame"); return 0; }
int main(int argc, char *argv[]) { int max = 5; stack_init(max); assert(stack_isempty()); stack_push(1); stack_push(2); stack_push(3); assert(stack_count() == 3); assert(stack_top() == 3); stack_push(3); assert(stack_count() == 3); assert(stack_top() == 3); stack_push(2); assert(stack_count() == 3); assert(stack_top() == 3); stack_push(4); stack_push(5); stack_push(5); // stack full, ok to push a duplicate item. nl(); printf("count: %d \n", stack_count()); return 0; }
static char *test_scoping() { bstring varname = bfromcstr("baz"); Object *obj1 = object_number(interpreter, 7); Object *set1 = interpreter_set_variable(interpreter, varname, obj1); mu_assert(set1 != NULL, "Failed to set local"); Interpreter *i1 = interpreter_enter_scope(interpreter); mu_assert(i1 != NULL, "Failed to enter scope"); mu_assert(stack_count(interpreter->scopes) == 2, "Should be 2 stack frame"); Object *obj2 = object_number(interpreter, 9); Object *set2 = interpreter_set_variable(interpreter, varname, obj2); mu_assert(set2 != NULL, "Failed to set local"); Object *get2 = interpreter_get_variable(interpreter, varname); mu_assert(get2 != NULL, "Failed to set global"); mu_assert(get2->number == 9, "Number was incorrect"); Interpreter *i2 = interpreter_leave_scope(interpreter); mu_assert(i2 != NULL, "Failed to leave scope"); Object *get3 = interpreter_get_variable(interpreter, varname); mu_assert(get3 != NULL, "Failed to set global"); mu_assert(get3->number == 7, "Number was incorrect"); return 0; }
static int gui_update() { int i, count; if (gui->need_update==1) { GuiWidget *cur_widget; cur_widget = (GuiWidget*)stack_get(gui->win_stack, 0); widget_update(cur_widget); count = stack_count(gui->win_stack); for (i=1; i<count -1; i++) { cur_widget = (GuiWidget *) stack_get(gui->win_stack, i); if (cur_widget && window_get_update(cur_widget) == 1) { widget_update(cur_widget); window_set_update(cur_widget, 0); } } widget_update(GET_TOP_WINDOW); SDL_Flip(screen); gui->need_update = 0; return 1; } return 0; }
void main() { int no, ch, e; printf("\n 1 - Push"); printf("\n 2 - Pop"); printf("\n 3 - Top"); printf("\n 4 - Empty"); printf("\n 5 - Exit"); printf("\n 6 - Dipslay"); printf("\n 7 - Stack Count"); printf("\n 8 - Destroy stack"); create(); while (1) { printf("\n Enter choice : "); scanf("%d", &ch); switch (ch) { case 1: printf("Enter data : "); scanf("%d", &no); push(no); break; case 2: pop(); break; case 3: if (top == NULL) printf("No elements in stack"); else { e = topelement(); printf("\n Top element : %d", e); } break; case 4: empty(); break; case 5: exit(0); case 6: display(); break; case 7: stack_count(); break; case 8: destroy(); break; default : printf(" Wrong choice, Please enter correct choice "); break; } } }
long double* stack_multipop(rpn_stack **stack, int size){ if(stack_count(*stack) < size){ return NULL; } long double *rv = (long double *) malloc(sizeof(long double) * size); for(int i=0; i<size; i++){ rv[i] = stack_pop(stack); } return rv; }
int main(){ stack_init(); printf("%d\n", stack_count()); stack_push(5); stack_push(2); stack_push(10); printf("%d\n", stack_count()); stack_dup(); stack_add(); printf("%d\n", stack_count()); int x; puts(""); while(!stack_empty()){ x = stack_pop(); printf("%d\n", x); } return 0; }
static char *test_scoping_error() { Interpreter *i1 = interpreter_leave_scope(interpreter); mu_assert(i1 != NULL, "Error leaving scope"); mu_assert(stack_count(interpreter->scopes) == 0, "Should be 0 stack frame"); Interpreter *i2 = interpreter_leave_scope(interpreter); mu_assert(i2 == NULL, "Expected error leaving scope"); mu_assert(interpreter->error == 1, "Interpreter should have errored"); interpreter_clear_error(interpreter); return 0; }
status_t _release_stack_chunks(stack_handle_t stack) { status_t status = NO_ERROR; size_t count = 0; void* chunk = NULL; if (NULL == stack) { return ERR_NULL_POINTER; } status = stack_count(stack, &count); while ((status == NO_ERROR) && (count > 0)) { status = stack_pop(stack, &chunk); if (NO_ERROR != status) { continue; } free(chunk); status = stack_count(stack, &count); } return status; }
static void stack_mul(void (*func)(unsigned char *, const unsigned char *, const unsigned char *)) { volatile unsigned char a; /* Mark the beginning of the stack */ write_canary(&a); (*func)(r,x,y); ctr = (int)&a - (int)&_end - stack_count(STACK_CANARY) - 21; /* 21 is 18 pushed caller registers + 3 from function call */ sprintf(str, "%7d", ctr); print(str); print(" bytes\n"); }
// to push an element onto stack void push(int value) { int count; struct node *temp; temp = (struct node*)malloc(sizeof(struct node)); count = stack_count(); if (count <= MAX - 1) { temp->datum = value; temp->next = top; top = temp; } else printf("WARNING: STACK FULL\n"); }
void stack_print(rpn_stack *stack){ rpn_stack *aux1 = stack, *aux2 = NULL; int count = stack_count(stack); while(aux1 != NULL){ stack_push(&aux2, aux1->value); aux1 = aux1->next; } stack_free(&aux1); while(aux2 != NULL){ printf("%i: %Lf\n", count, aux2->value); aux2 = aux2->next; count--; } stack_free(&aux2); }
int main(void) { volatile unsigned char a; /* Mark the beginning of the stack */ for(i=0;i<5;i++) { canary = random(); WRITE_CANARY(&a); crypto_verify(x,y); newctr =(unsigned int)&a - (unsigned int)&_end - stack_count(canary); ctr = (newctr>ctr)?newctr:ctr; } print_stack(XSTR(crypto_verify),0,ctr); avr_end(); return 0; }
bool is_valid(char *s) { bool valid = true; Stack stack; if(')' == *s || ']' == *s || ']' == *s) return false; stack_init(&stack); while(*s){ switch(*s){ case '(': case '[': case '{': stack_push(&stack, *s); break; case ')': if('(' != stack_pop(&stack)) { valid = false; } break; case ']': if('[' != stack_pop(&stack)) { valid = false; } break; case '}': if('{' != stack_pop(&stack)) { valid = false; } break; default: valid = false; break; } if(!valid) break; s++; } if(stack_count(&stack) > 0){ valid = false; } stack_destroy(&stack); return valid; }
HuffNode * readInput(char* filename) /*This function read the input file, and create a Huffman tree based on the command of the input file.*/ { FILE * inputfile = fopen(filename, "r");//open the file first if(inputfile == NULL){return NULL;}//if the file cannot be opened,return NULL pointer to indicate the user with an error message. int temp = fgetc(inputfile);//get the first character of the input file Stack * stack = NULL;//create a stack pointer with NULL content while(temp != EOF)//if the first character of the input is not end of the file, then we can run the following command. { if(temp == push)//if the command is push, then run the following. { temp = fgetc(inputfile);//get the value which needs to be pushed onto the stack if(temp == EOF)//if the value is end of the file, then there is something wrong with input file. { fclose(inputfile);//close the file and return NULL to indicate the user with an error message. return NULL; } HuffNode * temp_node = create_node(temp);//create a temparary Huffman node stack = stack_push(stack, temp_node);//push the node into the top of the stack } else if(temp == pop)//if the command is pop, then run the following { if(stack_count(stack) == 1){break;}//There must be two nodes on the stack to do the pop command.If not, then just stop and do nothing. HuffNode * right = stack_peek(stack);//check the what in the current stack, which contains the right child of the new Huffman node stack = stack_pop(stack);//then pop out the current stack HuffNode * left = stack_peek(stack);//do the same thing to check the left child of the new Huffman node stack = stack_pop(stack);//then pop out the nonused stack HuffNode * new_node = stack_node(left, right);//create a new HUffman node, and assign the left and right to the Huffmand node stack = stack_push(stack, new_node);//then push the new Huffman node on top of the stack } temp = fgetc(inputfile);//get the next character with file pointer } fclose(inputfile);//after reading all the input commands, close the input file HuffNode * input_list = stack_peek(stack);//get the node contained in the current stack destroyStack(stack);//deallocate the memory used to contain the stack return input_list; }
void print_memory(){ int s = stack_count(); int h = heap_count(); printf("\nSTACK:\n %d bytes used\n", s); printf("\n HEAP:\n %d pages used -> %d bytes\n\n", h, h*4096); }
void main(){ int op,n,resp; system("clear"); printf("|_____________Estrutura de dados_____________|\n"); printf("| TDA - PILHA |\n"); printf("|Alunos: |\n"); printf("| Alessandro Silva |\n"); printf("| Alexandre Leite |\n"); printf("| Edgard Pitombo |\n"); printf("| Roberto R. |\n"); printf("| |\n"); printf("|Professor: Danilo silva |\n"); printf("| 06/11/2014|\n"); printf("|____________________________________________|\n\n"); printf("_--__--__--__--__--__--__--__--__--__--__--__-\n\n"); while(op!=9){ printf("\n"); printf("|__________________MENU______________________|\n"); printf("| |\n"); printf("| 1)ADICIONAR elemento na pilha. |\n"); printf("| 2)REMOVER elemento do topo da pilha. |\n"); printf("| 3)Exibir TODOS os elementos da pilha. |\n"); printf("| 4)Exibir o elemento do TOPO da pilha. |\n"); printf("| 5)Exibir QUANTIDADE de elementos da pilha. |\n"); printf("| 6)LOCALIZAR um elemento na pilha. |\n"); printf("| 9)Para SAIR. |\n"); printf("|____________________________________________|\n"); printf("\nDigite uma opção: "); scanf("%d",&op); if(op!=9){ switch(op){ case 1: printf("Digite um valor inteiro:"); scanf("%d",&n); if(push(n)==1){ printf("Valor inserido na pilha."); display(); }else{ printf("Falha ao armazenar valor na pilha."); } break; case 2: resp=pop(); if(resp!=0){ printf("\nO elemento removido foi: %d \n", resp); display(); }else{ printf("A pilha está vazia!"); } break; case 3: display(); break; case 4: printf("\nO elemento do topo da pilha é: %d\n",topelement()); display(); break; case 5: printf("\nPilha possui %d elementos\n", stack_count()); display(); break; case 6: printf("Digite o elemento que deseja procurar: "); scanf("%d",&n); localiza(n); display(); break; } } } }
void* _memory_pool_thread(void* data) { memory_pool_t* pool = (memory_pool_t*)data; int doKill = 0; int err = 0; status_t status = NO_ERROR; size_t chunkCount = 0; size_t availableChunks = 0; void* chunk = NULL; if (NULL == pool) { pthread_exit(NULL); } while (0 == doKill) { /* update whether to kill thread or not */ err = pthread_mutex_lock(&(pool->mutex)); if (err) { pthread_exit(NULL); } doKill = pool->kill_thread; err = pthread_mutex_unlock(&(pool->mutex)); if (err) { pthread_exit(NULL); } if (doKill) { /* skip rest of loop and exit while loop */ continue; } /* get number of chunks available */ err = pthread_mutex_lock(&(pool->mutex)); if (err) { pthread_exit(NULL); } status = stack_count(pool->available, &availableChunks); if (NO_ERROR != status) { pthread_exit(NULL); } err = pthread_mutex_unlock(&(pool->mutex)); if (err) { pthread_exit(NULL); } /* determine if more chunks are needed */ if (pool->min_reserve_chunks > availableChunks) { chunkCount = pool->max_reserve_chunks - availableChunks; while ( (chunkCount > 0) && ((chunkCount + pool->generated_chunks) > pool->max_chunks)) { chunkCount--; } while ((chunkCount != 0) && (status == NO_ERROR)) { status = _memory_pool_generate_chunk(pool, pool->generated); chunkCount--; } if (NO_ERROR != status) { pthread_exit(NULL); } } /* if chunks exist in generated stack, place them in available stack */ status = stack_count(pool->generated, &chunkCount); err = pthread_mutex_lock(&(pool->mutex)); if (err) { pthread_exit(NULL); } while ((NO_ERROR == status) && (chunkCount > 0)) { status = stack_pop(pool->generated, &chunk); if (NO_ERROR != status) { continue; } status = stack_push(pool->available, chunk); if (NO_ERROR != status) { continue; } chunkCount--; } err = pthread_mutex_unlock(&(pool->mutex)); if (err) { pthread_exit(NULL); } if (NO_ERROR != status) { pthread_exit(NULL); } /* sleep */ err = usleep(pool->period_microseconds); if (err) { pthread_exit(NULL); } } pthread_exit(NULL); }
int stack_count(Stack * stack) /*This function counts how many Huffman nodes are in the stack list.*/ { if(stack == NULL){return 0;}//base case: return 0 since there are no nodes anymore. return (1 + stack_count(stack -> next));//return the increasing counting number of the number of nodes in the stack }
// to check if stack is full short is_full() { int count = stack_count(); return (count >= MAX); }
int main(void) { volatile unsigned char a; /* Mark the beginning of the stack */ for(i=0;i<5;i++) { canary = random(); WRITE_CANARY(&a); crypto_sign_keypair(pk,sk); newctr =(unsigned int)&a - (unsigned int)&_end - stack_count(canary); ctr = (newctr>ctr)?newctr:ctr; } print_stack(XSTR(crypto_sign_keypair),-1,ctr); for(i=0;i<5;i++) { canary = random(); WRITE_CANARY(&a); crypto_sign(sm,&smlen,sm,0,sk); newctr =(unsigned int)&a - (unsigned int)&_end - stack_count(canary); ctr = (newctr>ctr)?newctr:ctr; } print_stack(XSTR(crypto_sign),0,ctr); for(i=0;i<5;i++) { canary = random(); WRITE_CANARY(&a); crypto_sign_open(sm,&mlen,sm,smlen,pk); newctr =(unsigned int)&a - (unsigned int)&_end - stack_count(canary); ctr = (newctr>ctr)?newctr:ctr; } print_stack(XSTR(crypto_sign_open),smlen,ctr); for(j=1;j<=MAXTEST_BYTES;j<<=1) { mlen = j; for(i=0;i<5;i++) { canary = random(); WRITE_CANARY(&a); crypto_sign(sm,&smlen,sm,mlen,sk); newctr =(unsigned int)&a - (unsigned int)&_end - stack_count(canary); ctr = (newctr>ctr)?newctr:ctr; } print_stack(XSTR(crypto_sign),mlen,ctr); for(i=0;i<5;i++) { canary = random(); WRITE_CANARY(&a); crypto_sign_open(sm,&mlen,sm,smlen,pk); newctr =(unsigned int)&a - (unsigned int)&_end - stack_count(canary); ctr = (newctr>ctr)?newctr:ctr; } print_stack(XSTR(crypto_sign_open),smlen,ctr); } avr_end(); return 0; }