Question: All in C programming Language. You will write two different implementations of the data type for this assignment. First, implement a set using an unsorted

All in C programming Language.

You will write two different implementations of the data type for this assignment. First, implement a set using an unsorted array of length m > 0, in which the first n <= m slots are used to hold n strings in some arbitrary order. Use sequential search to locate an element in the array. Second, implement a set using a sorted array of length m > 0, in which the first n <= m slots are used to hold n strings in ascending order. Use binary search to locate an element in the array. For both implementations, rather than duplicating the search logic in several functions, write an auxiliary function search that returns the location of an element in an array. Declare search as static so it is not visible outside the source file. Use search to implement the functions in the interface. Your implementation should allocate memory and copy the string when adding, and therefore also deallocate memory when removing. Note that unique takes an optional second file, whose words it removes from the set, thus allowing you to test insertion followed by deletion. In contrast, parity interweaves insertion and deletion, so it is a tougher test. You should use the assert macro defined in where appropriate.

/**************************************************Interface*********************************************/

# ifndef SET_H

# define SET_H

The interface to your abstract data type must provide the following operations: SET *createSet(int maxElts); return a pointer to a new set with amaximumcapacity of maxElts void destroySet(SET *sp); deallocate memory associated with the set pointed to by sp int numElements(SET *sp); return the number of elements in the set pointed to by sp void addElement(SET *sp, char *elt); add elt to the set pointed to by sp void removeElement(SET *sp, char *elt); remove elt from the set pointed to by sp char *findElement(SET *sp, char *elt); if elt is present in the set pointed to by sp then return thematching element, otherwise return NULL char **getElements(SET *sp); allocate and return an array of elements in the set pointed to by sp

# endif /* SET_H */

/******************************************Parity.c*************************************************/

# include

# include

# include

# include "set.h"

# define MAX_SIZE 18000

/*

* Function: main

*

* Description: Driver function for the test application.

*/

int main(int argc, char *argv[])

{

FILE *fp;

char buffer[BUFSIZ];

/******************************************unique.c****************************************************/

# include

# include

# include

# include

# include "set.h"

# define MAX_SIZE 18000

/*

* Function: main

*

* Description: Driver function for the test application.

*/

int main(int argc, char *argv[])

{

FILE *fp;

char buffer[BUFSIZ], **elts;

SET *unique;

int i, words;

bool lflag = false;

/* Check usage and open the first file. */

if (argc > 1 && strcmp(argv[1], "-l") == 0) {

lflag = true;

argc --;

for (i = 1; i < argc; i ++)

argv[i] = argv[i + 1];

}

if (argc == 1 || argc > 3) {

fprintf(stderr, "usage: %s [-l] file1 [file2] ", argv[0]);

exit(EXIT_FAILURE);

}

if ((fp = fopen(argv[1], "r")) == NULL) {

fprintf(stderr, "%s: cannot open %s ", argv[0], argv[1]);

exit(EXIT_FAILURE);

}

/* Insert all words into the set. */

words = 0;

unique = createSet(MAX_SIZE);

while (fscanf(fp, "%s", buffer) == 1) {

words ++;

addElement(unique, buffer);

}

fclose(fp);

if (!lflag) {

printf("%d total words ", words);

printf("%d distinct words ", numElements(unique));

}

/* Try to open the second file. */

if (argc == 3) {

if ((fp = fopen(argv[2], "r")) == NULL) {

fprintf(stderr, "%s: cannot open %s ", argv[0], argv[2]);

exit(EXIT_FAILURE);

}

/* Delete all words in the second file. */

while (fscanf(fp, "%s", buffer) == 1)

removeElement(unique, buffer);

fclose(fp);

if (!lflag)

printf("%d remaining words ", numElements(unique));

}

/* Print the list of words if desired. */

if (lflag) {

elts = getElements(unique);

for (i = 0; i < numElements(unique); i ++)

printf("%s ", elts[i]);

free(elts);

}

destroySet(unique);

exit(EXIT_SUCCESS);

}

SET *odd;

int words;

/* Check usage and open the file. */

if (argc != 2) {

fprintf(stderr, "usage: %s file1 ", argv[0]);

exit(EXIT_FAILURE);

}

if ((fp = fopen(argv[1], "r")) == NULL) {

fprintf(stderr, "%s: cannot open %s ", argv[0], argv[1]);

exit(EXIT_FAILURE);

}

/* Insert or delete words to compute their parity. */

words = 0;

odd = createSet(MAX_SIZE);

while (fscanf(fp, "%s", buffer) == 1) {

words ++;

if (findElement(odd, buffer))

removeElement(odd, buffer);

else

addElement(odd, buffer);

}

printf("%d total words ", words);

printf("%d words occur an odd number of times ", numElements(odd));

fclose(fp);

destroySet(odd);

exit(EXIT_SUCCESS);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!