/* param: stack the stack being manipulated pre: the stack contains at least two elements post: the top two elements are popped and the second is raised to the power of the first and is pushed back onto the stack. */ void power(struct DynArr *stack) { //declare necessary doubles for operator use on stack double operand_1, operand_2, pow_result; //make sure the stack is not empty assert(!isEmptyDynArr(stack)); //store and pop first number off operand_1 = topDynArr(stack); popDynArr(stack); //make sure stack is not empty assert(!isEmptyDynArr(stack)); //store and pop second number off operand_2 = topDynArr(stack); popDynArr(stack); //store in result pow_result = pow(operand_2, operand_1); //push back onto stack pushDynArr(stack, pow_result); }
/* Checks whether the (), {}, and [] are balanced or not param: s pointer to a string pre: s is not null post: */ int isBalanced(char* s) { char c; DynArr *Parens; Parens = newDynArr(5); // Arbitrary first length //printf("DEBUG 40 s = %p\n", s); while ( (c = nextChar(s)) != '\0' ) { if (EQ(c, '(')) pushDynArr(Parens, ')'); else if (EQ(c, '{')) pushDynArr(Parens, '}'); else if (EQ(c, '[')) pushDynArr(Parens, ']'); else if (EQ(c, ')') || EQ(c,'}') || EQ(c, ']')) { if(isEmptyDynArr(Parens)) return 0; // We already know we're unbalanced else if ( EQ(topDynArr(Parens), c)) popDynArr(Parens); } } if (isEmptyDynArr(Parens)) return 1; // True, is balanced else return 0; // False, is not balanced }
/* Checks whether the (), {}, and [] are balanced or not param: s pointer to a string pre: s is not null post: */ int isBalanced(char* s) { char check; char cmp; DynArr *stck; stck = newDynArr(100); do { check = nextChar(s); if (check == '(' || check == '{' || check == '[') { pushDynArr(stck, check); } else if (check == ')' || check == '}' || check == ']') { if (isEmptyDynArr(stck)) { return 0; } cmp = topDynArr(stck); if ((check == ')' && cmp == '(') || (check == '}' && cmp == '{') || (check == ']' && cmp == '[')) { popDynArr(stck); } } } while (check != '\0'); if (isEmptyDynArr(stck)) { return 1; } else return 0; }
/* Checks whether the (), {}, and [] are balanced or not param: s pointer to a string pre: s is not null post: */ int isBalanced(char* s) { /* FIXME: You will write this function */ DynArr* work; work = newDynArr(sizeof(s)); char testChar = 0; do{ testChar = nextChar(s); if (testChar == '(') { pushDynArr(work, ')'); } else if (testChar == ')') { if (isEmptyDynArr(work)) { return 0; } else if (testChar != topDynArr(work)) { return 0; } else popDynArr(work); } else if (testChar == '[') { pushDynArr(work, ']'); } else if (testChar == ']') { if (isEmptyDynArr(work)) { return 0; } else if (testChar != topDynArr(work)) { return 0; } else popDynArr(work); } else if (testChar == '{') { pushDynArr(work, '}'); } else if (testChar == '}') { if (isEmptyDynArr(work)) { return 0; } else if (testChar != topDynArr(work)) { return 0; } else popDynArr(work); } } while (testChar != '\0'); if (!isEmptyDynArr(work)) return 0; return 1; }
/* Returns the element at the top of the stack param: v pointer to the dynamic array pre: v is not null pre: v is not empty post: no changes to the stack */ TYPE topDynArr(DynArr *v) { assert(v != 0); assert(!isEmptyDynArr(v)); //make sure the array is not empty return v->data[sizeDynArr(v) - 1]; //return element at the top of the stack }
/* Removes the element on top of the stack param: v pointer to the dynamic array pre: v is not null pre: v is not empty post: size is decremented by 1 the top has been removed */ void popDynArr(DynArr *v) { assert(v != 0); assert(!isEmptyDynArr(v)); removeAtDynArr(v, sizeDynArr(v) - 1); }
/* Removes the element on top of the stack param: v pointer to the dynamic array pre: v is not null pre: v is not empty post: size is decremented by 1 the top has been removed */ void popDynArr(DynArr *v) { /* FIXME: You will write this function */ if (isEmptyDynArr(v) == 0) { v->size--; } }
/* Removes the element on top of the stack param: v pointer to the dynamic array pre: v is not null pre: v is not empty post: size is decremented by 1 the top has been removed */ void popDynArr(DynArr *v) { /* FIXME: You will write this function */ assert(v != NULL); assert(!isEmptyDynArr(v)); v->size--; }
/* Returns the element at the top of the stack param: v pointer to the dynamic array pre: v is not null pre: v is not empty post: no changes to the stack */ TYPE topDynArr(DynArr *v) { /* FIXME: You will write this function */ /* FIXME: You will change this return value*/ if (isEmptyDynArr(v) == 0) { return v->data[v->size-1]; } }
/* Checks whether the (), {}, and [] are balanced or not param: s pointer to a string pre: s is not null post: */ int isBalanced(char* s) { /* FIXME: You will write this function */ /* Check if s is null */ assert(s); /* Create stack to hold letters */ DynArr* stack = newDynArr(16); char c = nextChar(s); while(c) { if(c == '(' || c == '{' || c == '[') pushDynArr(stack, c); else if(c == ')' || c == '}' || c == ']') { int balanced = 0; if(!isEmptyDynArr(stack)) { char top = topDynArr(stack); if(top == '(' && c == ')') balanced = 1; else if(top == '{' && c == '}') balanced = 1; else if(top == '[' && c == ']') balanced = 1; } if(balanced) popDynArr(stack); else return 0; } c = nextChar(s); } return isEmptyDynArr(stack); }
/* Returns the element at the top of the stack param: v pointer to the dynamic array pre: v is not null pre: v is not empty post: no changes to the stack */ TYPE topDynArr(DynArr *v) { /* FIXME: You will write this function */ assert(v != NULL); assert(!isEmptyDynArr(v)); /* FIXME: You will change this return value*/ return v->data[v->size - 1]; }
/* Get the first node, which has the min priority, from the heap param: heap pointer to the heap pre: heap is not empty ret: value of first node */ TYPE getMinHeap(DynArr *heap) { /* FIXME */ /* Temporary returning NULL */ // return NULL; assert(!isEmptyDynArr(heap)); return getDynArr(heap,0); }
/* Remove the first node, which has the min priority, from the heap param: heap pointer to the heap pre: heap is not empty post: the first node is removed from the heap */ void removeMinHeap(DynArr *heap) { /* FIXME */ assert(!isEmptyDynArr(heap)); heap->data[0] = getDynArr(heap, (sizeDynArr(heap) - 1)); heap->size--; _adjustHeap(heap, (sizeDynArr(heap) - 1), 0); }
/* Put an item into the dynamic array at the specified location, overwriting the element that was there param: v pointer to the dynamic array param: pos the index to put the value into param: val the value to insert pre: v is not null pre: v is not empty pre: pos >= 0 and pos < size of the array post: index pos contains new value, val */ void putDynArr(DynArr *v, int pos, TYPE val) { /* FIXME: You will write this function */ if (isEmptyDynArr(v) == 0) { if((pos < (sizeDynArr(v))) && (pos >= 0)) { v->data[pos] = val; } } }
TYPE getDynArr(DynArr *v, int pos) { /* FIXME: You will write this function */ /* FIXME: you must change this return value */ if (isEmptyDynArr(v) == 0) { if((pos < (sizeDynArr(v))) && (pos >= 0)) { return v->data[pos]; } } }
/* Returns the element at the top of the stack param: v pointer to the dynamic array pre: v is not null pre: v is not empty post: no changes to the stack */ TYPE topDynArr(DynArr *v) { /* DONE: You will write this function */ assert (v != NULL); assert (! isEmptyDynArr(v)); return v->data[(v->size - 1)]; }
/* Put an item into the dynamic array at the specified location, overwriting the element that was there param: v pointer to the dynamic array param: pos the index to put the value into param: val the value to insert pre: v is not null pre: v is not empty pre: pos >= 0 and pos < size of the array post: index pos contains new value, val */ void putDynArr(DynArr *v, int pos, TYPE val) { /* DONE: You will write this function */ assert (v != NULL ); // v is not null assert (! isEmptyDynArr(v)); // v is not empty assert (pos < v->size ); assert (pos >= 0 ); v->data[pos] = val; }
void _buildHeap(DynArr *heap) { /* FIXME */ assert(!isEmptyDynArr(heap)); int max = heap->size - 1; for(int i = max/ 2; i >= 0; i--) _adjustHeap(heap, max, i); }
/* Returns boolean (encoded as an int) demonstrating whether or not the specified value is in the collection true = 1 false = 0 param: v pointer to the dynamic array param: val the value to look for in the bag pre: v is not null pre: v is not empty post: no changes to the bag */ int containsDynArr(DynArr *v, TYPE val) { int i = 0; assert(!isEmptyDynArr(v)); for(i = 0; i < sizeDynArr(v); i++) if(EQ(v->data[i], val) ) return 1; return 0; }
/* Removes the first occurrence of the specified value from the collection if it occurs param: v pointer to the dynamic array param: val the value to remove from the array pre: v is not null pre: v is not empty post: val has been removed post: size of the bag is reduced by 1 */ void removeDynArr(DynArr *v, TYPE val) { /* FIXME: You will write this function */ if (isEmptyDynArr(v) == 0) { int i; for (i = 0; i<sizeDynArr(v); i++) { if (v->data[i] == val) { removeAtDynArr(v, i); } } } }
/* Returns boolean (encoded as an int) demonstrating whether or not the specified value is in the collection true = 1 false = 0 param: v pointer to the dynamic array param: val the value to look for in the bag pre: v is not null pre: v is not empty post: no changes to the bag */ int containsDynArr(DynArr *v, TYPE val) { assert(v != 0); assert(!isEmptyDynArr(v)); if (findElement(v, val) != -1) { return 1; } return 0; }
TYPE getDynArr(DynArr *v, int pos) { /* DONE: You will write this function */ assert (v != NULL); assert (! isEmptyDynArr(v)); assert (pos < v->size ); assert (pos >= 0 ); return v->data[pos]; }
/* Returns boolean (encoded as an int) demonstrating whether or not the specified value is in the collection true = 1 false = 0 param: v pointer to the dynamic array param: val the value to look for in the bag pre: v is not null pre: v is not empty post: no changes to the bag */ int containsDynArr(DynArr *v, TYPE val) { int i = 0; assert(v!=0); assert(!isEmptyDynArr(v)); for(i = 0; i < sizeDynArr(v); i++) if(compare(v->data[i], val) == 0) return 1; return 0; }
/* Swap two specified elements in the dynamic array param: v pointer to the dynamic array param: i,j the elements to be swapped pre: v is not null pre: v is not empty pre: i, j >= 0 and i,j < size of the dynamic array post: index i now holds the value at j and index j now holds the value at i */ void swapDynArr(DynArr *v, int i, int j) { /* FIXME: You will write this function */ if (isEmptyDynArr(v) == 0) { int size = sizeDynArr(v); if (i>=0 && j >=0 && i<size && j<size) { TYPE pos; pos = v->data[i]; v->data[i] = v->data[j]; v->data[j] = pos; } } }
/* Removes the first occurrence of the specified value from the collection if it occurs param: v pointer to the dynamic array param: val the value to remove from the array pre: v is not null pre: v is not empty post: val has been removed post: size of the bag is reduced by 1 */ void removeDynArr(DynArr *v, TYPE val) { int i = 0; assert(!isEmptyDynArr(v)); for(i = 0; i < sizeDynArr(v); i++) if(EQ(v->data[i], val)) { removeAtDynArr(v,i); break; } }
/* Removes the first occurrence of the specified value from the collection if it occurs param: v pointer to the dynamic array param: val the value to remove from the array pre: v is not null pre: v is not empty post: val has been removed post: size of the bag is reduced by 1 */ void removeDynArr(DynArr *v, TYPE val) { int i = 0; assert(!isEmptyDynArr(v)); for(i = 0; i < sizeDynArr(v); i++) if(EQ(v->data[_absoluteIndex(v,i)], val)) { removeAtDynArr(v,i); /* RemoveAt will get absolute index */ break; } }
/* Removes the first occurrence of the specified value from the collection if it occurs param: v pointer to the dynamic array param: val the value to remove from the array pre: v is not null pre: v is not empty post: val has been removed post: size of the bag is reduced by 1 */ void removeDynArr(DynArr *v, TYPE val) { assert(v != 0); assert(!isEmptyDynArr(v)); int index = findElement(v, val); //find the given element in the bag //remove that element from the bag if possible if (index != -1) { removeAtDynArr(v, index); } }
/* Removes the first occurrence of the specified value from the collection if it occurs param: v pointer to the dynamic array param: val the value to remove from the array pre: v is not null pre: v is not empty post: val has been removed post: size of the bag is reduced by 1 */ void removeDynArr(DynArr *v, TYPE val) { int i = 0; assert(v!=0); assert(!isEmptyDynArr(v)); assert(containsDynArr(v,val)); /* Design decision: Error if they try to remove something not in there! */ for(i = 0; i < sizeDynArr(v); i++) if(compare(v->data[i], val) == 0) { removeAtDynArr(v,i); break; } }
/* Removes the first occurrence of the specified value from the collection if it occurs param: v pointer to the dynamic array param: val the value to remove from the array pre: v is not null pre: v is not empty post: val has been removed post: size of the bag is reduced by 1 */ void removeDynArr(DynArr *v, TYPE val) { /* FIXME: You will write this function */ assert(v != NULL); assert(!isEmptyDynArr(v)); for (int i = 0; i < v->size; i++) { if (v->data[i] == val) { removeAtDynArr(v, i); break; } } }
/* Remove the element at the specified location from the array, shifts other elements back one to fill the gap param: v pointer to the dynamic array param: idx location of element to remove pre: v is not null pre: v is not empty pre: idx < size and idx >= 0 post: the element at idx is removed post: the elements past idx are moved back one */ void removeAtDynArr(DynArr *v, int idx) { /* FIXME: You will write this function */ if (isEmptyDynArr(v) == 0) { int max = sizeDynArr(v); if (idx < max && idx >= 0) { int i; for (i = idx; i<max-1; i++) { v->data[i] = v->data[i+1]; //element at idx position is removed } v->size--; //size reflects this } } }