void test_pop_element_from_stack(){ int _5 = 5,_6 = 6; Stack* integerStack = createStack(3); ASSERT(push(integerStack,&_5)); ASSERT(push(integerStack, &_6)); ASSERT(_6 == *(int*)pop(integerStack)); // dispose(integerStack); }
int main() { Queue* q = (Queue*)malloc(sizeof(Queue)); q->s1 = createStack(10, 1); q->s2 = createStack(10, 2); int i; for(i = 1 ; i < 10 ; i++) { insert(&q, i); } printf("queue:%d\n", qRemove(&q)); printf("queue:%d\n", qRemove(&q)); printf("queue:%d\n", qRemove(&q)); printf("queue:%d\n", qRemove(&q)); return 0; }
void core_handle(string str, int n, int k) { cout << "for string " << str << endl; Stack<char> s = createStack(str); cout << s.toString() << endl; roll(s, n, k); cout << s.toString() << endl << endl; }
void calc(char *inf) { if (isDigit (inf[0]) || inf[0] == '(') { int i = 0; char c = '\0'; bool hasBracket = false; Stack * head = createStack(); while ((inf[i] != '\0')) { if (isOp(inf[i])) { printf(" "); if (!isEmpty(head)) { c = pop(head); if (c == '(') //again, check if bracket (it's second, first is down there) push(head, c); else if (isHigher(inf[i], c)) //check if operand has higher priority push(head, c); else { while (!isEmpty(head)) { printf("%c ", c); c = pop(head); } printf("%c ", c); } } push(head, inf[i]); } else if (isDigit(inf[i])) // check if number printf("%c", inf[i]); else //check if bracket if (inf[i] == '(') push(head, inf[i]); else //push all out till ( if (inf[i] == ')') { c = pop(head); while ((c != '(') && (!isEmpty(head))) { printf("%c ", c); c = pop(head); } } i++; } while (!isEmpty(head)) printf("%c ", pop(head)); } else printf("error"); }
char* exp2post(char* exp) { Stack* stack = createStack(tamanhoaux); char top, str; char* convertedExp = (char*) malloc(tamanhoaux*sizeof(char)); int i=0, p=-1, j=0; while(exp[i]!='\0') { if (!peek(stack,&top)) p=-1; else p=prioridade(top); str = exp[i]; if(((str >= 'A')&&(str <= 'z')) || (((str>='0')&&(str<='9')))) { convertedExp[j] = str; j++; } else if(str=='(') { push(stack,str); } else if(str==')') { push(stack,str); top=')'; while(top!='(') { pop(stack,&top); if(top!='('&&top!=')') { convertedExp[j]=top; j++; } } } else if (prioridade(str)<=p) { pop(stack,&top); convertedExp[j]=top; j++; push(stack,str); } else if(prioridade(str)>p) { push(stack,str); } i++; } while(!isEmptyStack(stack)) { pop(stack,&top); convertedExp[j]=top; j++; } convertedExp[j] = '\0'; return convertedExp; }
int evaluateExpression(char *expression){ Token *token; Stack *numberStack=createStack(); Stack *operatorStack=createStack(); if(expression ==NULL){ Throw(ERR_NO_ARGUMENT); } Text *newText=textNew(expression); String *tokenizer = stringNew(newText); token=getToken(tokenizer); if(token->type == IDENTIFIER_TOKEN){ Throw(ERR_NOT_ACCEPT_IDENTIFIER); }else{ while(token!=NULL){ if(isOperator(token)){ if(((Operator*)token)->info->affix!=PREFIX) tryConvertToPrefix((Operator*)token); stackPush(token,operatorStack); }else if(isNumber(token)){ stackPush(token,numberStack); break; }else Throw(ERR_INVALID_IDENTIFIER); token=getToken(tokenizer); if(token==NULL) break; } } while((token=getToken(tokenizer))!=NULL ){ evaluatePostfixesPrefixesAndInfixes(expression,token,numberStack,operatorStack); } evaluateAllOperatorOnStack(numberStack,operatorStack); Number *result=(Number*)stackPop(numberStack); destroyStack(numberStack); if(operatorStack !=NULL){ destroyStack(operatorStack); } return result->value; }
Stack create_chars(int length, char *array){ int i; Stack charData = *createStack(sizeof(char), length); for (i = 0; i < charData.total_elements; ++i) { ((char *)charData.starting_address)[i] = array[i]; } return charData; }
Stack create_strings(int length, string array){ int i; Stack strData = *createStack(sizeof(string), length); for (i = 0; i < strData.total_elements; i++) { (*(string *)strData.starting_address)[i] = *array+i*sizeof(string); } return strData; }
int main() { Stack* s=createStack(); push(s,1); int data = pop(s); printf("%d", data); deleteStack(s); return 0; }
void test_2_create_a_new_char_type_Stack(){ Stack data , expected ; char temp[] = {0,0,0,0,0}; expected.starting_address = temp; expected.each_size = sizeof(char); expected.total_elements = 5; data = *createStack(sizeof(char), 5); ASSERT(1 == areEqual(data, expected)); }
void test_3_create_a_new_string_type_Stack(){ Stack data , expected ; string temp[] = {"","",""}; expected.starting_address = temp; expected.each_size = sizeof(string); expected.total_elements = 3; data = *createStack(sizeof(string), 3); ASSERT(1 == areEqual(data, expected)); }
int main() { stack *S = createStack(10); // printArray(input, argc-1); tree* root = makeTree(input, argc-1); inOrderNonRecursive(S, root); showTreeStack(S); shutDown(S, root, input); return 0; }
Stack create_ints(int length, int *array){ int i; Stack intData = *createStack(sizeof(int), length); for (i = 0; i < intData.total_elements; ++i) { ((int *)intData.starting_address)[i] = array[i]; } return intData; }
int main() { struct Stack* stack = createStack(2); pop(stack); push(stack, 10); push(stack, 20); push(stack, 30); pop(stack); return 0; }
void preorderIterative (struct Node *root){ struct Stack* s1 = createStack(); struct Stack* s2 = createStack(); struct Node* temp; push(&s1,root); while(s1){ temp = pop(s1); push(&s2,temp); if(root->left) push(&s1,root->left); if(root->right) push(&s2,root->right); } while(s2){ temp= pop(s2); cout << temp->data << endl; } }
int main(){ Stack *stack = createStack("Some data for the top of the stack"); push(stack, "Some more data"); printf("%s\n", stack->first->value); printf("%s\n", stack->first->next->value); void* data = pop(stack); printf("%s\n", data); return 0; }
void test_evaluatePrefixesAndNumber_evaluate_divide_multiply_should_throw_error_expecting_number(void){ CEXCEPTION_T e; Token *token; Stack *numberStack=createStack(); Stack *operatorStack=createStack(); Text *newText=textNew("/*"); String *tokenizer = stringNew(newText); token=getToken(tokenizer); token=getToken(tokenizer); Try { evaluatePrefixesAndNumber("/*",token,numberStack,operatorStack); } Catch(e) { TEST_ASSERT_EQUAL(ERR_CANNOT_CONVERT_TO_PREFIX,e); } }
void test_1_create_a_new_int_type_Stack(){ Stack* data; Stack expected; int temp[] = {0,0,0,0,0}; expected.starting_address = temp; expected.each_size = sizeof(int); expected.total_elements = 5; data = createStack(sizeof(int), 5); ASSERT(1 == areEqual(*data, expected)); }
int main(){ Stack * stack1; stack1 = createStack(); Element *e1, *e2, *e3, *e4, *e5; int a, b, c, d, e; a = 1; b = 2; c = 3; e1 = createElement(&a); e2 = createElement(&b); e3 = createElement(&c); int empty = isEmpty(stack1); if (empty == 1) printf("The stack is empty!\n"); else printf("Why is the stack not empty?\n"); /* Add a Element to the stack and check if the stack is empty*/ push(stack1, e1); empty = isEmpty(stack1); if (empty == 1) printf("Why is the stack empty?\n"); else printf("The stack is not empty!\n"); /* Add another Element to the stack and check it is at the top by calling peek() */ push(stack1, e2); Element * test; test = peek(stack1); printf("The top element is: %d\n", *(int *)(test->elementPtr)); /* remove the top element and test with peek() */ test = pop(stack1); test = peek(stack1); printf("The top element is: %d\n", *(int *)(test->elementPtr)); /* Add another and check with peek() */ push(stack1, e3); test = peek(stack1); printf("The top element is: %d\n", *(int *)(test->elementPtr)); destroyStack(stack1); return 0; }
void test_evaluatePrefixesAndNumber_evaluate_negative_positive_should_throw_error_expecting_number(void){ CEXCEPTION_T e; Token *token; Stack *numberStack=createStack(); Stack *operatorStack=createStack(); Text *newText=textNew("-+"); String *tokenizer = stringNew(newText); token=getToken(tokenizer); token=getToken(tokenizer); token=getToken(tokenizer); Try { evaluatePrefixesAndNumber("-+",token,numberStack,operatorStack); } Catch(e) { TEST_ASSERT_EQUAL(ERR_EXPECTING_NUMBER,e); } }
void test_evaluatePrefixesAndNumber_evaluate_should_push_negative_into_operatorStack_and_5_into_number_stack(void){ Token *token; Stack *numberStack=createStack(); Stack *operatorStack=createStack(); Text *newText=textNew("-5"); String *tokenizer = stringNew(newText); //- token=getToken(tokenizer); evaluatePrefixesAndNumber("-5",token,numberStack,operatorStack); TEST_ASSERT_NOT_NULL(token); TEST_ASSERT_EQUAL_STRING("-",((Operator*)token)->info->symbol); TEST_ASSERT_EQUAL(MINUS_OP,((Operator*)token)->info->id); //5 token=getToken(tokenizer); evaluatePrefixesAndNumber("-5",token,numberStack,operatorStack); TEST_ASSERT_NOT_NULL(token); TEST_ASSERT_EQUAL(5,((Number*)token)->value); }
interpreteer * initializeInterpreteer ( instruction * chain ) { interpreteer * mainInt = ( interpreteer * ) malloc ( sizeof ( interpreteer ) ); mainInt->spine = ( stack * ) malloc ( sizeof ( stack ) ); mainInt->virtualMem = ( myMemory * ) malloc ( sizeof ( myMemory ) ); mainInt->instrList = chain; mainInt->spine = createStack (); mainInt->virtualMem->isAllocated = 0; initializeMem ( mainInt->virtualMem ); return mainInt; }
Stack push(Stack s, Dictionnary d){ if(s->dict == NULL){ s->dict = d; return s; } Stack newNode = createStack(); newNode = malloc(sizeof(Stack)); newNode->dict = d; newNode->nxt = s; return newNode; }
int main() { struct stack* stack = createStack(100); push(stack,10); push(stack,30); push(stack,40); push(stack,50); push(stack,50); printf("%d popped from stack\n", pop(stack)); printf("Top item is %d\n", peek(stack)); return 0; }
WindNinjaTree::WindNinjaTree(QWidget *parent) : QWidget(parent) { createTree(); createStack(); createConnections(); layout = new QVBoxLayout; layout->addWidget(tree); layout->addWidget(stack); setLayout(layout); }
void test_evaluatePrefixesAndNumber_evaluate_multiply_should_throw_error_cannot_convert_to_prefix_operator(void){ CEXCEPTION_T e; Token *token; Stack *numberStack=createStack(); Stack *operatorStack=createStack(); Text *newText=textNew("*"); String *tokenizer = stringNew(newText); //( token=getToken(tokenizer); Try { evaluatePrefixesAndNumber("*",token,numberStack,operatorStack); TEST_FAIL_MESSAGE("Should throw Error no expression "); } Catch(e) { TEST_ASSERT_EQUAL(ERR_CANNOT_CONVERT_TO_PREFIX ,e); } }
void test_evaluatePrefixesAndNumber_evaluate_opening_bracket_7(void){ Token *token; Stack *numberStack=createStack(); Stack *operatorStack=createStack(); Text *newText=textNew("(7"); String *tokenizer = stringNew(newText); //( token=getToken(tokenizer); evaluatePrefixesAndNumber("(7",token,numberStack,operatorStack); TEST_ASSERT_NOT_NULL(token); TEST_ASSERT_EQUAL_STRING("(",((Operator*)token)->info->symbol); TEST_ASSERT_EQUAL(OPENING_BRACKET_OP,((Operator*)token)->info->id); //7 token=getToken(tokenizer); evaluatePrefixesAndNumber("(7",token,numberStack,operatorStack); TEST_ASSERT_NOT_NULL(token); TEST_ASSERT_EQUAL(7,((Number*)token)->value); }
int main() { STACKPTR s = createStack(5); push(s, 1); push(s, 2); push(s, 3); push(s, 4); Sort(s); while(!isEmpty(s)) { printf("%d", pop(s)); } return 0; }
int main() { struct Stack* stack = createStack(100); push(stack, 10); push(stack, 20); push(stack, 30); printf("%d popped from stack\n", pop(stack)); return 0; }
int convertBtoD(char *array){ int i, result = 0; Stack *stk = createStack(); for(i=0; array[i] != '\0'; i++){ stk = push((int)array[i]-48, stk); } while(!isEmpty(stk)){ result += pop(stk)*pow((double)2, (double)stk->currentSize); } return result; }