Ejemplo n.º 1
0
/* 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(v->size > 0);

	removeAtDynArr(v, sizeDynArr(v) - 1);
}
Ejemplo n.º 2
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)
{
	int last = heap->size - 1;
	heap->data[0] = heap->data[last];
	removeAtDynArr(heap, last);
	_adjustHeap(heap, heap->size, 0);
}
Ejemplo n.º 3
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)
{
	int last = sizeDynArr(heap) - 1;	
	putDynArr(heap,	0, getDynArr(heap, last));	
	removeAtDynArr(heap, last);
	_adjustHeap(heap, last,	0);	
}
Ejemplo n.º 4
0
/* 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);
}
Ejemplo n.º 5
0
/* 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)
{
	/* DONE: You will write this function */
	// This is just a special case of removeAtDynArr

  removeAtDynArr (v, v->size - 1);
}
Ejemplo n.º 6
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)
{
	for (int i = 0; i < v->size; i++) {
			if (val == v->data[i]) { /* found it */
				removeAtDynArr(v, i);
				return;
			}
		}
}
Ejemplo n.º 7
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, comparator compare)
{
	int last = sizeDynArr(heap)-1;
	//assert(last !=0); /*at least one element!*/
	
	putDynArr(heap,0,getDynArr(heap,last));
	removeAtDynArr(heap,last); /*remove last element*/
	_adjustHeap(heap,last,0, compare);
}
Ejemplo n.º 8
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)
{
    int last = sizeDynArr(heap) -1;
    if (last != 0) {
        heap->data[0] = heap->data[last];
    }
    removeAtDynArr(heap, last);
    _adjustHeap(heap, last, 0);
}
Ejemplo n.º 9
0
/* 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 != 0);
	assert(v->size != 0);

	removeAtDynArr(v, v->size-1);

	//v->size--;
}
Ejemplo n.º 10
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);
            }
        }
    }
}
Ejemplo n.º 11
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 */
								int i;
								for (i = 0; i < sizeDynArr(v); i++) {
																/* If the value is found we will remove it and end the for loop */
																if(EQ(val, v->data[i])) {
																								removeAtDynArr(v, i);
																								i = sizeDynArr(v) + 1;
																}
								}
}
Ejemplo n.º 12
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)
{
	int i = 0;

	assert(!isEmptyDynArr(v));

	for(i = 0; i < sizeDynArr(v); i++)
      if(EQ(v->data[i], val))
      {
           removeAtDynArr(v,i); 
           break;
      }
}
Ejemplo n.º 13
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)
{
	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;
      }
}
Ejemplo n.º 14
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)
{
	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);
	}
}
Ejemplo n.º 15
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 */
	    int last = sizeDynArr(heap)-1;
     assert (heap != 0); /* make sure we have at least one element */
               /* Copy the last element to the first  position */

	    if(last!=0)
      {putDynArr(heap, 0, getDynArr(heap, last)); }
    
      removeAtDynArr(heap, last);       /* Remove last element.*/
      _adjustHeap(heap, last-1, 0);/* Rebuild heap */

}
Ejemplo n.º 16
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 */
    assert(v != NULL);
    assert(!isEmptyDynArr(v));
    
    for (int i = 0; i < v->size; i++) {
        if (v->data[i] == val) {
            removeAtDynArr(v, i);
            break;
        }
    }
    
}
Ejemplo n.º 17
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)
{
	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;
      }
}
Ejemplo n.º 18
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)
{
	assert(v != 0);
	assert(v->size > 0);

	for(int i = 0; i < v->size; i++)
	{
		if(getDynArr(v, i) == val)
		{
			removeAtDynArr(v, i);
			return;
		}
	}

}
Ejemplo n.º 19
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)
{
    int last;
    assert( sizeDynArr( heap ) > 0 );
    last = sizeDynArr( heap ) - 1;

    /* Copy the last element to the first */
    putDynArr( heap, 0, getDynArr( heap, last ) );

    /* Remove last element */
    removeAtDynArr( heap, last );

    /* Rebuild heap */
    _adjustHeap( heap, last, 0 );
}
Ejemplo n.º 20
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 */
	assert(v != 0);
	assert(v->size != 0);

	for(int i = 0;i < v->size;i++)
	{
		if (v->data[i] == val)
            		removeAtDynArr(v, i);

	}




}
Ejemplo n.º 21
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)
{
	/* DONE: You will write this function */
	int i;

	assert (v != NULL);
	assert (! isEmptyDynArr(v));
	
	for (i=0; i < v->size; i++)
	{
		if (v->data[i] == val)
		{
  		removeAtDynArr (v, i);
			return;
		}
	}
}
Ejemplo n.º 22
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 (sizeDynArr(heap) > 0);

    int fin = (sizeDynArr(heap) - 1);   //last element

    /* swap the last element for the first, then remove the last position */
    swapDynArr(heap, fin, 0);

    removeAtDynArr(heap, fin);          //shifts any elements past last element to left and reduces size

    /* rebuild the heap */
    _adjustHeap(heap, fin, 0);

}
Ejemplo n.º 23
0
// this main function contains some
int main(int argc, char* argv[]){

	DynArr *dyn;
	dyn = createDynArr(2);
	
	printf("\n\nTesting addDynArr...\n");
	addDynArr(dyn, 3);
	addDynArr(dyn, 4);
	addDynArr(dyn, 10);
	addDynArr(dyn, 5);
	addDynArr(dyn, 6);
	
	
	printf("The array's content: [3,4,10,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 1), 4), "Test 2nd element == 4");
	assertTrue(EQ(getDynArr(dyn, 2), 10), "Test 3rd element == 10");
	assertTrue(EQ(getDynArr(dyn, 3), 5), "Test 4th element == 5");
	assertTrue(EQ(getDynArr(dyn, 4), 6), "Test 5th element == 6");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting putDynArr...\nCalling putDynArr(dyn, 2, 7)\n");
	putDynArr(dyn, 2, 7); 
	printf("The array's content: [3,4,7,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 7), "Test 3rd element == 7");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting swapDynArr...\nCalling swapDynArr(dyn, 2, 4)\n");
	swapDynArr(dyn, 2, 4);
	printf("The array's content: [3,4,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 6), "Test 3rd element == 6");
	assertTrue(EQ(getDynArr(dyn, 4), 7), "Test 5th element == 7");
	
	printf("\n\nTesting removeAtDynArr...\nCalling removeAtDynArr(dyn, 1)\n");
	removeAtDynArr(dyn, 1);
	printf("The array's content: [3,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 3), 7), "Test 4th element == 7");
	assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");
	
	printf("\n\nTesting stack interface...\n");
	printf("The stack's content: [3,6,5,7] <- top\n");
	assertTrue(!isEmptyDynArr(dyn), "Testing isEmptyDynArr");
	assertTrue(EQ(topDynArr(dyn), 7), "Test topDynArr == 7");
	
	popDynArr(dyn);
	printf("Poping...\nThe stack's content: [3,6,5] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 5), "Test topDynArr == 5");
	
	pushDynArr(dyn, 9);
	printf("Pushing 9...\nThe stack's content: [3,6,5,9] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 9), "Test topDynArr == 9");
	
	printf("\n\nTesting bag interface...\n");
	printf("The bag's content: [3,6,5,9]\n");
	assertTrue(containsDynArr(dyn, 3), "Test containing 3");
	assertTrue(containsDynArr(dyn, 6), "Test containing 6");
	assertTrue(containsDynArr(dyn, 5), "Test containing 5");
	assertTrue(containsDynArr(dyn, 9), "Test containing 9");
	assertTrue(!containsDynArr(dyn, 7), "Test not containing 7");
	
	removeDynArr(dyn, 3);
	printf("Removing 3...\nThe stack's content: [6,5,9]\n");
	assertTrue(!containsDynArr(dyn, 3), "Test not containing 3");
	
	printf("Executing test functions...\n");
	printf("\nTesting addDynArr()\n");
	addDynArr_TEST(dyn);  //Testing the add functionality under 3 conditions
	printf("\nTesting popDynArr()\n");
	popDynArr_TEST(dyn);  //Testing the pop function under 2 conditions
	printf("\nTesting topDynArr()\n");
	topDynArr_TEST(dyn);  //Testing the top function under 2 conditions
	printf("\nTesting pushDynArr()\n");
	pushDynArr_TEST(dyn); //Testing the push function under 3 conditions
	printf("\nTesting containsDynArr()\n");
	containsDynArr_TEST(dyn); //Testing the contains function under 2 conditions
	printf("\nTesting removeDynArr()\n"); 
	removeDynArr_TEST(dyn); //Testing the remove function under 2 conditions
	return 0;
}
Ejemplo n.º 24
0
/* 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)
{
    removeAtDynArr(v, v->size);
}
Ejemplo n.º 25
0
/* 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 */
								/* removing the top value from stack */
								removeAtDynArr(v, v->size - 1);
}
Ejemplo n.º 26
0
// this main function contains some
int main(int argc, char* argv[]){

	DynArr *dyn;
	dyn = newDynArr(2);
        int i; 

	printf("\n\nTesting addDynArr...\n");
	addDynArr(dyn, 3);
	addDynArr(dyn, 4);
	addDynArr(dyn, 10);
	addDynArr(dyn, 5);
	addDynArr(dyn, 6);
	
	printf("The array's content: [3,4,10,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 1), 4), "Test 2nd element == 4");
	assertTrue(EQ(getDynArr(dyn, 2), 10), "Test 3rd element == 10");
	assertTrue(EQ(getDynArr(dyn, 3), 5), "Test 4th element == 5");
	assertTrue(EQ(getDynArr(dyn, 4), 6), "Test 5th element == 6");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting putDynArr...\nCalling putDynArr(dyn, 2, 7)\n");
	putDynArr(dyn, 2, 7); 
	printf("The array's content: [3,4,7,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 7), "Test 3rd element == 7");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting swapDynArr...\nCalling swapDynArr(dyn, 2, 4)\n");
	swapDynArr(dyn, 2, 4);
	printf("The array's content: [3,4,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 6), "Test 3rd element == 6");
	assertTrue(EQ(getDynArr(dyn, 4), 7), "Test 5th element == 7");
	
	printf("\n\nTesting removeAtDynArr...\nCalling removeAtDynArr(dyn, 1)\n");
	removeAtDynArr(dyn, 1);
	printf("The array's content: [3,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 3), 7), "Test 4th element == 7");
	assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");
	
	printf("\n\nTesting stack interface...\n");
	printf("The stack's content: [3,6,5,7] <- top\n");
	assertTrue(!isEmptyDynArr(dyn), "Testing isEmptyDynArr");
	assertTrue(EQ(topDynArr(dyn), 7), "Test topDynArr == 7");
	
	popDynArr(dyn);
	printf("Poping...\nThe stack's content: [3,6,5] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 5), "Test topDynArr == 5");
	
	pushDynArr(dyn, 9);
	printf("Pushing 9...\nThe stack's content: [3,6,5,9] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 9), "Test topDynArr == 9");
	
	printf("\n\nTesting bag interface...\n");
	printf("The bag's content: [3,6,5,9]\n");
	assertTrue(containsDynArr(dyn, 3), "Test containing 3");
	assertTrue(containsDynArr(dyn, 6), "Test containing 6");
	assertTrue(containsDynArr(dyn, 5), "Test containing 5");
	assertTrue(containsDynArr(dyn, 9), "Test containing 9");
	assertTrue(!containsDynArr(dyn, 7), "Test not containing 7");
	
	removeDynArr(dyn, 3);
	printf("Removing 3...\nThe stack's content: [6,5,9]\n");
	assertTrue(!containsDynArr(dyn, 3), "Test not containing 3");
	
	return 0;
}
Ejemplo n.º 27
0
/* 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)
{
	removeAtDynArr(v,sizeDynArr(v)-1);
}
Ejemplo n.º 28
0
void removeDynArrIter (struct DynArrIter *itr) {
	itr->cur--;
	removeAtDynArr(itr->da , itr->cur);
}
Ejemplo n.º 29
0
void removeDynArrOrd(DynArr *v, TYPE val, comparator compare)
{
  int idx = _binarySearch(v, val, compare);
  if(EQ(v->data[idx], val))
    removeAtDynArr(v, idx);
}