Question

Header file #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 #ifndef __TYPE #define __TYPE # define TYPE int #...

Header file

#ifndef DYNAMIC_ARRAY_INCLUDED
#define DYNAMIC_ARRAY_INCLUDED 1

#ifndef __TYPE
#define __TYPE
# define TYPE int
# define TYPE_SIZE sizeof(int)
# endif

# ifndef LT
# define LT(A, B) ((A) < (B))
# endif

# ifndef EQ
# define EQ(A, B) ((A) == (B))
# endif

typedef struct DynArr DynArr;

/* Dynamic Array Functions */
void initDynArr(DynArr *v, int capacity);
DynArr *newDynArr(int cap);

void freeDynArr(DynArr *v);
void deleteDynArr(DynArr *v);

int sizeDynArr(DynArr *v);

void addDynArr(DynArr *v, TYPE val);
TYPE getDynArr(DynArr *v, int pos);
void putDynArr(DynArr *v, int pos, TYPE val);
void swapDynArr(DynArr *v, int i, int j);
void removeAtDynArr(DynArr *v, int idx);

/* Stack interface. */
int isEmptyDynArr(DynArr *v);
void pushDynArr(DynArr *v, TYPE val);
TYPE topDynArr(DynArr *v);
void popDynArr(DynArr *v);

/* Bag Interface */
/* Note addDynArr is already declared above*/
int containsDynArr(DynArr *v, TYPE val);
void removeDynArr(DynArr *v, TYPE val);

#endif

dynArray file

#include <assert.h>

#include <stdlib.h>

#include <stdio.h>

#include "dynArray.h"

struct DynArr

{

TYPE *data; /* pointer to the data array */

int size; /* Number of elements in the array */

int capacity; /* capacity ofthe array */

};

/* ************************************************************************

Dynamic Array Functions

************************************************************************ */

/* Initialize (including allocation of data array) dynamic array.

param: v pointer to the dynamic array

param: cap capacity of the dynamic array

pre: v is not null

post: internal data array can hold cap elements

post: v->data is not null

*/

void initDynArr(DynArr *v, int capacity)

{

assert(capacity > 0);

assert(v != 0);

v->data = (TYPE *)malloc(sizeof(TYPE) * capacity);

assert(v->data != 0);

v->size = 0;

v->capacity = capacity;

}

/* Allocate and initialize dynamic array.

param: cap desired capacity for the dyn array

pre: none

post: none

ret: a non-null pointer to a dynArr of cap capacity

and 0 elements in it.

*/

DynArr* newDynArr(int cap)

{

assert(cap > 0);

DynArr *r = (DynArr *)malloc(sizeof(DynArr));

assert(r != 0);

initDynArr(r, cap);

return r;

}

/* Deallocate data array in dynamic array.

param: v pointer to the dynamic array

pre: none

post: d.data points to null

post: size and capacity are 0

post: the memory used by v->data is freed

*/

void freeDynArr(DynArr *v)

{

if (v->data != 0)

{

free(v->data); /* free the space on the heap */

v->data = 0; /* make it point to null */

}

v->size = 0;

v->capacity = 0;

}

/* Deallocate data array and the dynamic array ure.

param: v pointer to the dynamic array

pre: none

post: the memory used by v->data is freed

post: the memory used by d is freed

*/

void deleteDynArr(DynArr *v)

{

freeDynArr(v);

free(v);

}

/* Resizes the underlying array to be the size cap

param: v pointer to the dynamic array

param: cap the new desired capacity

pre: v is not null

post: v has capacity newCap

*/

void _dynArrSetCapacity(DynArr *v, int newCap)

{

TYPE *temp = (TYPE*)malloc(sizeof(TYPE) * newCap);

int i;

for (i = 0; i < v->size; i++)

{

temp[i] = v->data[i];

}

free(v->data); //free old array

v->data = temp; //assign address new array

v->size = newCap; //assign address new array

}

/* Get the size of the dynamic array

param: v pointer to the dynamic array

pre: v is not null

post: none

ret: the size of the dynamic array

*/

int sizeDynArr(DynArr *v)

{

return v->size;

}

/* Adds an element to the end of the dynamic array

param: v pointer to the dynamic array

param: val the value to add to the end of the dynamic array

pre: the dynArry is not null

post: size increases by 1

post: if reached capacity, capacity is doubled

post: val is in the last utilized position in the array

*/

void addDynArr(DynArr *v, TYPE val)

{

assert(v != 0);

v->size = v->size + 1;

if (v->size == v->capacity)

{

_dynArrSetCapacity(v, 2 * v->capacity);

}

v->data[v->size] = val;

v->size++;

}

/* Get an element from the dynamic array from a specified position

param: v pointer to the dynamic array

param: pos integer index to get the element from

pre: v is not null

pre: v is not empty

pre: pos < size of the dyn array and >= 0

post: no changes to the dyn Array

ret: value stored at index pos

*/

TYPE getDynArr(DynArr *v, int pos)

{

assert(v != 0);

assert(pos < v->size && pos >= 0);

assert(!isEmptyDynArr(v));

/* FIXME: You will write this function */

if (pos < v->size && pos >= 0)

{

return v->data[pos];

}

else

/* FIXME: you must change this return value */

return 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)

{

/* FIXME: You will write this function */

assert(v != 0 && pos >= 0 && pos < v->size);

assert(!isEmptyDynArr(v));

v->data[pos] = val;

}

/* 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 */

assert(v != 0 && i >= 0 && j >= 0 && i < v->size && j < v->size);

assert(!isEmptyDynArr(v));

TYPE temp;

temp = v->data[i];

v->data[i] = v->data[j];

v->data[j] = temp; //swap of values

}

/* 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 */

assert(v != 0 && idx < v->size && idx >= 0);

assert(!isEmptyDynArr(v));

int i;

for (i = idx; i < v->size; i++)

{

v->data[idx] = v->data[idx + 1]; //shift elements to fill gap.

}

v->size--;

}

/* ************************************************************************

Stack Interface Functions

************************************************************************ */

/* Returns boolean (encoded in an int) demonstrating whether or not the

dynamic array stack has an item on it.

param: v pointer to the dynamic array

pre: the dynArr is not null

post: none

ret: 1 if empty, otherwise 0

*/

int isEmptyDynArr(DynArr *v)

{

/* FIXME: You will write this function */

assert(v != 0);

if (v->size == 0)

{

return 1;

}

/* FIXME: You will change this return value*/

else return 0;

}

/* Push an element onto the top of the stack

param: v pointer to the dynamic array

param: val the value to push onto the stack

pre: v is not null

post: size increases by 1

if reached capacity, capacity is doubled

val is on the top of the stack

*/

void pushDynArr(DynArr *v, TYPE val)

{

/* FIXME: You will write this function */

assert(v != 0);

addDynArr(v, val);

}

/* 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 != 0);

assert(!isEmptyDynArr(v));

/* FIXME: You will change this return value*/

return v->data[v->size - 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 */

assert(v != 0);

assert(!isEmptyDynArr(v));

v->size--;

}

/* ************************************************************************

Bag Interface Functions

************************************************************************ */

/* 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)

{

/* FIXME: You will write this function */

assert(v != 0);

assert(!isEmptyDynArr(v));

int i;

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 */

assert(val != 0);

assert(!isEmptyDynArr(v));

int i;

for (i = 0; i < sizeDynArr(v); i++)

{

if (EQ(v->data[i], val))

{

removeAtDynArr(v, i);

break;

}

}

}

void _printDynArr(struct DynArr *da)

{

int i;

for (i = 0; i < da->size; i++)

printf("DA[%d] == %d\n", i, da->data[i]);

}

test .c file

#include <stdio.h>

#include <stdlib.h>

#include "dynArray.h"

void assertTrue(int predicate, char *message)

{

printf("%s: ", message);

if (predicate)

printf("PASSED\n");

else

printf("FAILED\n");

}

// 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");

deleteDynArr(dyn);

return 0;

}

The code in the .c file needs to past the tests on the testDynArray item. The header file can't be altered and the functions can't be altered other than the code in them. Please use the same variables.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code was almost correct. Few minor bugs which I fixed. I have highlighted the fixes in bold. All the tests pass.

If happy with the answer, please rate it.

1. in _dynArrSetCapacity() , the newCap should be assigned to capacity and not size.

2. in addDynArr(), the size was incremented even before adding the element, I deleted that line. The size is incremented in the end after adding it to array

3. in removeAtDynArr(), instead of using i for indexing to move elements in the for loop , you had used idx itself, which would move only one and the same element at idx all the time the loop executes. i.e instead of using v->data[i] = v->data[i+1] , you used v->data[idx] = v->data[idx+1]

#include <assert.h>

#include <stdlib.h>

#include <stdio.h>

#include "dynArray.h"

struct DynArr

{

TYPE *data; /* pointer to the data array */

int size; /* Number of elements in the array */

int capacity; /* capacity ofthe array */

};

/* ************************************************************************

Dynamic Array Functions

************************************************************************ */

/* Initialize (including allocation of data array) dynamic array.

param: v pointer to the dynamic array

param: cap capacity of the dynamic array

pre: v is not null

post: internal data array can hold cap elements

post: v->data is not null

*/

void initDynArr(DynArr *v, int capacity)

{

assert(capacity > 0);

assert(v != 0);

v->data = (TYPE *)malloc(sizeof(TYPE) * capacity);

assert(v->data != 0);

v->size = 0;

v->capacity = capacity;

}

/* Allocate and initialize dynamic array.

param: cap desired capacity for the dyn array

pre: none

post: none

ret: a non-null pointer to a dynArr of cap capacity

and 0 elements in it.

*/

DynArr* newDynArr(int cap)

{

assert(cap > 0);

DynArr *r = (DynArr *)malloc(sizeof(DynArr));

assert(r != 0);

initDynArr(r, cap);

return r;

}

/* Deallocate data array in dynamic array.

param: v pointer to the dynamic array

pre: none

post: d.data points to null

post: size and capacity are 0

post: the memory used by v->data is freed

*/

void freeDynArr(DynArr *v)

{

if (v->data != 0)

{

free(v->data); /* free the space on the heap */

v->data = 0; /* make it point to null */

}

v->size = 0;

v->capacity = 0;

}

/* Deallocate data array and the dynamic array ure.

param: v pointer to the dynamic array

pre: none

post: the memory used by v->data is freed

post: the memory used by d is freed

*/

void deleteDynArr(DynArr *v)

{

freeDynArr(v);

free(v);

}

/* Resizes the underlying array to be the size cap

param: v pointer to the dynamic array

param: cap the new desired capacity

pre: v is not null

post: v has capacity newCap

*/

void _dynArrSetCapacity(DynArr *v, int newCap)

{

TYPE *temp = (TYPE*)malloc(sizeof(TYPE) * newCap);

int i;

for (i = 0; i < v->size; i++)

{

temp[i] = v->data[i];

}

free(v->data); //free old array

  

v->data = temp; //assign address new array

v->capacity = newCap; //assign address new array

}

/* Get the size of the dynamic array

param: v pointer to the dynamic array

pre: v is not null

post: none

ret: the size of the dynamic array

*/

int sizeDynArr(DynArr *v)

{

return v->size;

}

/* Adds an element to the end of the dynamic array

param: v pointer to the dynamic array

param: val the value to add to the end of the dynamic array

pre: the dynArry is not null

post: size increases by 1

post: if reached capacity, capacity is doubled

post: val is in the last utilized position in the array

*/

void addDynArr(DynArr *v, TYPE val)

{

assert(v != 0);

  

if (v->size == v->capacity)

{

_dynArrSetCapacity(v, 2 * v->capacity);

}

v->data[v->size] = val;

v->size++;

  

}

/* Get an element from the dynamic array from a specified position

param: v pointer to the dynamic array

param: pos integer index to get the element from

pre: v is not null

pre: v is not empty

pre: pos < size of the dyn array and >= 0

post: no changes to the dyn Array

ret: value stored at index pos

*/

TYPE getDynArr(DynArr *v, int pos)

{

assert(v != 0);

assert(pos < v->size && pos >= 0);

assert(!isEmptyDynArr(v));

  

/* FIXME: You will write this function */

if (pos < v->size && pos >= 0)

{

return v->data[pos];

}

else

  

/* FIXME: you must change this return value */

return -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)

{

/* FIXME: You will write this function */

assert(v != 0 && pos >= 0 && pos < v->size);

assert(!isEmptyDynArr(v));

  

v->data[pos] = val;

  

}

/* 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 */

assert(v != 0 && i >= 0 && j >= 0 && i < v->size && j < v->size);

assert(!isEmptyDynArr(v));

  

TYPE temp;

  

temp = v->data[i];

v->data[i] = v->data[j];

v->data[j] = temp; //swap of values

  

}

/* 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 */

assert(v != 0 && idx < v->size && idx >= 0);

assert(!isEmptyDynArr(v));

int i;

for (i = idx; i < v->size -1; i++)

{

v->data[i] = v->data[i + 1]; //shift elements to fill gap.

}

v->size--;

}

/* ************************************************************************

Stack Interface Functions

************************************************************************ */

/* Returns boolean (encoded in an int) demonstrating whether or not the

dynamic array stack has an item on it.

param: v pointer to the dynamic array

pre: the dynArr is not null

post: none

ret: 1 if empty, otherwise 0

*/

int isEmptyDynArr(DynArr *v)

{

/* FIXME: You will write this function */

assert(v != 0);

  

if (v->size == 0)

{

return 1;

}

  

/* FIXME: You will change this return value*/

else return 0;

}

/* Push an element onto the top of the stack

param: v pointer to the dynamic array

param: val the value to push onto the stack

pre: v is not null

post: size increases by 1

if reached capacity, capacity is doubled

val is on the top of the stack

*/

void pushDynArr(DynArr *v, TYPE val)

{

/* FIXME: You will write this function */

assert(v != 0);

addDynArr(v, val);

  

}

/* 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 != 0);

assert(!isEmptyDynArr(v));

/* FIXME: You will change this return value*/

return v->data[v->size - 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 */

assert(v != 0);

assert(!isEmptyDynArr(v));

v->size--;

}

/* ************************************************************************

Bag Interface Functions

************************************************************************ */

/* 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)

{

/* FIXME: You will write this function */

assert(v != 0);

assert(!isEmptyDynArr(v));

int i;

  

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 */

assert(val != 0);

assert(!isEmptyDynArr(v));

  

int i;

for (i = 0; i < sizeDynArr(v); i++)

{

if (EQ(v->data[i], val))

{

removeAtDynArr(v, i);

break;

}

}

}

void _printDynArr(struct DynArr *da)

{

int i;

for (i = 0; i < da->size; i++)

printf("DA[%d] == %d\n", i, da->data[i]);

}

output

Testing addDynArr...

The array's content: [3,4,10,5,6]

Test 1st element == 3: PASSED

Test 2nd element == 4: PASSED

Test 3rd element == 10: PASSED

Test 4th element == 5: PASSED

Test 5th element == 6: PASSED

Test size = 5: PASSED

Testing putDynArr...

Calling putDynArr(dyn, 2, 7)

The array's content: [3,4,7,5,6]

Test 3rd element == 7: PASSED

Test size = 5: PASSED

Testing swapDynArr...

Calling swapDynArr(dyn, 2, 4)

The array's content: [3,4,6,5,7]

Test 3rd element == 6: PASSED

Test 5th element == 7: PASSED

Testing removeAtDynArr...

Calling removeAtDynArr(dyn, 1)

The array's content: [3,6,5,7]

Test 1st element == 3: PASSED

Test 4th element == 7: PASSED

Test size = 4: PASSED

Testing stack interface...

The stack's content: [3,6,5,7] <- top

Testing isEmptyDynArr: PASSED

Test topDynArr == 7: PASSED

Poping...

The stack's content: [3,6,5] <- top

Test topDynArr == 5: PASSED

Pushing 9...

The stack's content: [3,6,5,9] <- top

Test topDynArr == 9: PASSED

Testing bag interface...

The bag's content: [3,6,5,9]

Test containing 3: PASSED

Test containing 6: PASSED

Test containing 5: PASSED

Test containing 9: PASSED

Test not containing 7: PASSED

Removing 3...

The stack's content: [6,5,9]

Test not containing 3: PASSED

Add a comment
Know the answer?
Add Answer to:
Header file #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 #ifndef __TYPE #define __TYPE # define TYPE int #...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This assignment is comprised of 3 parts: ​All files needed are located at the end of...

    This assignment is comprised of 3 parts: ​All files needed are located at the end of the directions. Part 1: Implementation of Dynamic Array, Stack, and Bag First, complete the Worksheets 14 (Dynamic Array), 15 (Dynamic Array Amortized Execution Time Analysis), 16 (Dynamic Array Stack), and 21 (Dynamic Array Bag). These worksheets will get you started on the implementations, but you will NOT turn them in. ​Do Not Worry about these, they are completed. Next, complete the dynamic array and...

  • All the functions are written and work besides removeLinkedList and contains LinkedList. Please fill those out. 무#ifndef #define LINKEDLIST-H LINKEDLISTH define TYPE /*# define TYPE SIZE sizeof (TYP...

    All the functions are written and work besides removeLinkedList and contains LinkedList. Please fill those out. 무#ifndef #define LINKEDLIST-H LINKEDLISTH define TYPE /*# define TYPE SIZE sizeof (TYPE) */ double typedef struct ListStack LinkedList; void initLinkedLǐst(LinkedList *1); void freeLinkedList (LinkedList *1) int İsEmptyLinkedList (LinkedList*1); void pushLinkedList (LinkedList #1, TYPE val); TYPE topLinkedList (LinkedList 1) void popLinkedList (LinkedList *1); /* Bag / void addLinkedList (LinkedList *1, TYPE val) ; int containsLinkedList (LinkedList *1, TYPE val) ; void removeLinkedList (LinkedList l, TYPE...

  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • Can you help with this C programming question. I have provided the skeleton code below along...

    Can you help with this C programming question. I have provided the skeleton code below along with the Stack/Data/Process Class for you to see/reference. Along with the Stack/Data type definition.   **SKELTON CODE** #include #include #include Stack* concat_stack(Stack *s1, Stack *s2) { //your code here return NULL; } **STACK CLASS FOR YOU TO REFERENCE** #include #include #include #include Stack* create_stack(int stack_capacity) { Stack *s = (Stack*) malloc(sizeof(Stack)); if (stack_capacity < 1) { fprintf(stderr, "Error(create_stack): invalid capacity, set to 10\n"); s->capacity =...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • Hashtable in C Please fix void ht_put(hashtable_t *ht, char *key, void *val) and void free_hashtable(hashtable_t *ht)...

    Hashtable in C Please fix void ht_put(hashtable_t *ht, char *key, void *val) and void free_hashtable(hashtable_t *ht) Implement void ht_del(hashtable_t *ht, char *key) and void ht_rehash(hashtable_t *ht, unsigned long newsize) --------------[hashtable.h]--------------------------------------- -######################################### #ifndef HASHTABLE_T #define HASHTABLE_T typedef struct hashtable hashtable_t; typedef struct bucket bucket_t; struct bucket { char *key; void *val; bucket_t *next; }; struct hashtable { unsigned long size; bucket_t **buckets; }; unsigned long hash(char *str); hashtable_t *make_hashtable(unsigned long size); void ht_put(hashtable_t *ht, char *key, void *val); void *ht_get(hashtable_t *ht,...

  • a7q3.cc File #include <iostream> #include <cstring> #include "ArrayList.h" using namespace std; // Algorithm copy(s) // Pre:...

    a7q3.cc File #include <iostream> #include <cstring> #include "ArrayList.h" using namespace std; // Algorithm copy(s) // Pre: s :: refToChar // Post: memory allocated on heap to store a copy // Return: reference to new string char *copy(char *s) { char *temp = new char[strlen(s)+1]; strcpy(temp,s); return temp; } void test_ListOperations(){    cout << "testing createList" << endl;    List *myList = createList(10);    if (myList == NULL){ cout << "createList failed" << endl; return; } else{ cout << "createList succeeded"...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT