Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(I DID MAJORITY OF THE CODE BUT GET STUCK ON UNION, INTERSECT AND SYMMETRIC DIFF

ID: 3572474 • Letter: #

Question

(I DID MAJORITY OF THE CODE BUT GET STUCK ON UNION, INTERSECT AND SYMMETRIC DIFFERENT. COULD I PLEASE GET SOME HELP!!!! ALL CODE ARE WRITTEN IN C LANGUAGE)

In this program you will represent sets of strings as linked lists. Each node will hold one string and the strings will appear in alphabetical order in the list. Thus the set containing the strings "one", "two" and "three" will be represented as:

To demonstrate how your "list sets" work you will write a simple set calculator. This calculator will store 10 sets (initially empty -- that is having a dummy node only) and have operations to add a string, remove a string, compute the union of two sets, compute the intersection of two sets, compute the symmetric difference of two sets (i.e. all elements which are in one or the other of the sets but not both), copy a set, clear a set (remove all strings) and display a set using normal set notation (e.g. "{one, three, two}"). The calculator will display a menu with the choices and accept an operation from the user. The calculator will then ask for the relevant set numbers (0 through 9) to work on and perform the operation. One run of the calculator is shown here:

Internals

You will write code to manipulate sets represented as above using linked lists. None of the required operations is difficult but some thought is in order. Special care must be taken to make sure that you free() all memory allocated with malloc(). This can be very tricky. You should check for memory leaks with valgrind though you will not be required to submit your valgrind results.

The calculator will consist of an array of head pointers, each indicating the beginning of a set. Initially each pointer will point to a dummy node and there will be no other nodes in the lists (this indicates empty sets). The set operations are the ones you learned in middle school and should require no further explanation.

All lists are kept in alphabetical order. Adding a string to a list will require that you place it in the proper alphabetical position. To do this you may use the following function which is a replacement for strcmp(). (Remember to #include .)

If an attempt is made to insert a string into a set which already contains that string the insertion will be ignored: strings in a set must be unique. Two string with different use of upper and lower case will be considered the same: for example "snargle" and "SnaRgle" are the same string.

Note: do not use "union" as an identifier in your program -- it is a keyword in C.

Validation

All user input must be validated. The user's choice of operation will be entered as as single character (scanf(" %c", &choice);). If the choice is invalid your program will prompt for another choice. If the user asks for a set whose number is not in the range 0 through 9 a polite error message will be given and the user will be asked to try again.

------------------------------------------------------------------------------------

Here's my code so far:

#include

#include

#include

#include

typedef struct _set

{

char str[20];

struct _set* next;

} Set;

void printMenu();

void addStr();

void removeStr();

Set *uniteSet();

void intersectSet();

void symmDiffSet();

Set *copySet();

void clearSet();

void printSet(Set *);

int strcmpa(char *, char *);

int main()

{

char in[10];

char setIn[10];

int i;

char choice;

int setChoice;

char strChoice[20];

Set* set[10];

for(i = 0; i < 10; i++)

set[i] = NULL;

while(1)

{

i = 0;

printMenu();

fgets(in, 10, stdin);

sscanf(in, "%c", &choice);

if(choice == 'a' || choice == 'A')

{

printf("set: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice);

for(i = 0; i < 10; i++)

{

if(setChoice == (i+1))

addStr(&set[i], strChoice);

}

if(setChoice < 1 || setChoice > 10)

printf("Please choose a set from 1-10. ");

}

else if(choice == 'r' || choice == 'R')

{

printf("set: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice);

for(i = 0; i < 10; i++)

{

if(setChoice == (i+1))

removeStr(&set[i]);

}

if(setChoice < 1 || setChoice > 10)

printf("Please choose a set from 1-10. ");

}

else if(choice == 'u' || choice == 'U')

{

int destChoice;

int setChoice2;

char destIn[10];

printf("destination set: ");

fgets(destIn, 10, stdin);

sscanf(destIn, "%d", &destChoice);

printf("source 1: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice);

printf("source 2: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice2);

set[(destChoice-1)] = uniteSet(set[(setChoice-1)], set[(setChoice2-1)]);

printf("ioansdoinasd ");

}

else if(choice == 'i' || choice == 'I')

{

intersectSet();

}

else if(choice == 's' || choice == 'S')

{

symmDiffSet();

}

else if(choice == 'c' || choice == 'C')

{

int destChoice;

char destIn[10];

printf("destination set: ");

fgets(destIn, 10, stdin);

sscanf(destIn, "%d", &destChoice);

printf("source: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice);

set[(destChoice-1)] = copySet(set[(setChoice-1)]);

}

else if(choice == 'z' || choice == 'Z')

{

printf("set: ");

fgets(in, 10, stdin);

sscanf(in, "%d", &setChoice);

  

for(i = 0; i < 10; i++)

{

if(setChoice == (i+1))

clearSet(set[i]);

}

if(setChoice < 1 || setChoice > 10)

printf("Please choose a set from 1-10. ");

}

else if(choice == 'p' || choice == 'P')

{

printf("set: ");

fgets(in, 10, stdin);

sscanf(in, "%d", &setChoice);

for(i = 0; i < 10; i++)

{

if(setChoice == (i+1))

printSet(set[i]);

}

if(setChoice < 1 || setChoice > 10)

printf("Please choose a set from 1-10. ");

}

else if(choice == 'q' || choice == 'Q')

break;

else

printf("Please choose an option from the menu. ");

}

return 0;

}

void printMenu()

{

printf("add string (a) remove string (r) ");

printf("union (u) intersection (i) ");

printf("symm. diff. (s) copy (c) ");

printf("clear set (z) print set (p) ");

printf("quit (q) -> ");

}

void addStr(Set** head)

{

char strIn[20];

char newStr[20];

printf("string: ");

fgets(strIn, 20, stdin);

sscanf(strIn, "%s", &newStr);

Set* set = (Set*)malloc(sizeof(Set));

if(set == NULL)

{

printf("ERROR! Node is null.");

exit(1);

}

Set* temp;

strcpy(set->str, newStr);

set->next = NULL;

if(*head == NULL)

{

set->next = *head;

*head = set;

}

else if(strcmpa((*head)->str, newStr) > 0)

{

set->next = *head;

*head = set;

}

else if(strcmpa((*head)->str, newStr) == 0)

{

printf("That string is already in the set. ");

}

else

{

temp = *head;

while((temp->next != NULL) && (strcmpa(temp->next->str, newStr)) < 0)

temp = temp->next;

set->next = temp->next;

temp->next = set;

}

}

void removeStr(Set **head)

{

char strIn[20];

char rmStr[20];

printf("string: ");

fgets(strIn, 20, stdin);

sscanf(strIn, "%s", &rmStr);

if(strcmpa((*head)->str, rmStr) == 0)

{

Set *temp = *head;

*head = (*head)->next;

free(temp);

return;

}

Set *current = (*head)->next;

Set *prev = *head;

while(current != NULL && prev != NULL)

{

if(strcmpa(current->str, rmStr) == 0)

{

Set *temp = current;

prev->next = current->next;

free(temp);

return;

}

prev = current;

current = current->next;

}

return;

}

Set *uniteSet(Set *set1, Set *set2)

{

Set *mergedSet = (Set *)malloc(sizeof(Set));

if(mergedSet == NULL)

{

printf("ERROR! mergedSet is null.");

exit(1);

}

if(set1 == NULL && set2 == NULL)

return NULL;

if(set1 == NULL)

return set2;

if(set2 == NULL)

return set1;

if(strcmpa(set1->str, set2->str) > 0)

mergedSet = set1;

else

mergedSet = set2;

while(set1 != NULL && set2 != NULL)

{

if(strcmpa(set1->str, set2->str) == 0)

{

mergedSet->next = set1;

set1 = set1->next;

}

else

{

mergedSet->next = set2;

set2 = set2->next;

}

}

if(set1 == NULL)

mergedSet->next = set2;

else

mergedSet->next = set1;

return mergedSet;

}

void intersectSet()

{

}

void symmDiffSet()

{

}

Set *copySet(Set *setSource)

{

if(setSource == NULL)

return NULL;

Set *dest = (Set *)malloc(sizeof(Set));

if(dest == NULL)

{

printf("ERROR! mergedSet is null. ");

exit(1);

}

strcpy(dest->str, setSource->str);

dest->next = copySet(setSource->next);

return dest;

}

void clearSet(Set *head)

{

while(head != NULL)

{

Set *temp = head;

head = temp->next;

temp->next = NULL;

free(temp);

}

}

void printSet(Set *head)

{

printf("{");

while(head != NULL)

{

printf("%s", head->str);

if(head->next != NULL)

printf(", ");

head = head->next;

}

printf("} ");

}

int strcmpa(char *s1, char *s2)

{

   while (*s1 && tolower(*s1) == tolower(*s2))

   {

s1++;

s2++;

   }

   return tolower(*s1) - tolower(*s2);

}

head dummy one three two

Explanation / Answer

#include

#include

#include

#include

typedef struct _set

{

char str[20];

struct _set* next;

} Set;

void printMenu();

void addStr();

void removeStr();

Set *uniteSet();

void intersectSet();

void symmDiffSet();

Set *copySet();

void clearSet();

void printSet(Set *);

int strcmpa(char *, char *);

int main()

{

char in[10];

char setIn[10];

int i;

char choice;

int setChoice;

char strChoice[20];

Set* set[10];

for(i = 0; i < 10; i++)

set[i] = NULL;

while(1)

{

i = 0;

printMenu();

fgets(in, 10, stdin);

sscanf(in, "%c", &choice);

if(choice == 'a' || choice == 'A')

{

printf("set: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice);

for(i = 0; i < 10; i++)

{

if(setChoice == (i+1))

addStr(&set[i], strChoice);

}

if(setChoice < 1 || setChoice > 10)

printf("Please choose a set from 1-10. ");

}

else if(choice == 'r' || choice == 'R')

{

printf("set: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice);

for(i = 0; i < 10; i++)

{

if(setChoice == (i+1))

removeStr(&set[i]);

}

if(setChoice < 1 || setChoice > 10)

printf("Please choose a set from 1-10. ");

}

else if(choice == 'u' || choice == 'U')

{

int destChoice;

int setChoice2;

char destIn[10];

printf("destination set: ");

fgets(destIn, 10, stdin);

sscanf(destIn, "%d", &destChoice);

printf("source 1: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice);

printf("source 2: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice2);

set[(destChoice-1)] = uniteSet(set[(setChoice-1)], set[(setChoice2-1)]);

printf("ioansdoinasd ");

}

else if(choice == 'i' || choice == 'I')

{

intersectSet();

}

else if(choice == 's' || choice == 'S')

{

symmDiffSet();

}

else if(choice == 'c' || choice == 'C')

{

int destChoice;

char destIn[10];

printf("destination set: ");

fgets(destIn, 10, stdin);

sscanf(destIn, "%d", &destChoice);

printf("source: ");

fgets(setIn, 10, stdin);

sscanf(setIn, "%d", &setChoice);

set[(destChoice-1)] = copySet(set[(setChoice-1)]);

}

else if(choice == 'z' || choice == 'Z')

{

printf("set: ");

fgets(in, 10, stdin);

sscanf(in, "%d", &setChoice);

  

for(i = 0; i < 10; i++)

{

if(setChoice == (i+1))

clearSet(set[i]);

}

if(setChoice < 1 || setChoice > 10)

printf("Please choose a set from 1-10. ");

}

else if(choice == 'p' || choice == 'P')

{

printf("set: ");

fgets(in, 10, stdin);

sscanf(in, "%d", &setChoice);

for(i = 0; i < 10; i++)

{

if(setChoice == (i+1))

printSet(set[i]);

}

if(setChoice < 1 || setChoice > 10)

printf("Please choose a set from 1-10. ");

}

else if(choice == 'q' || choice == 'Q')

break;

else

printf("Please choose an option from the menu. ");

}

return 0;

}

void printMenu()

{

printf("add string (a) remove string (r) ");

printf("union (u) intersection (i) ");

printf("symm. diff. (s) copy (c) ");

printf("clear set (z) print set (p) ");

printf("quit (q) -> ");

}

void addStr(Set** head)

{

char strIn[20];

char newStr[20];

printf("string: ");

fgets(strIn, 20, stdin);

sscanf(strIn, "%s", &newStr);

Set* set = (Set*)malloc(sizeof(Set));

if(set == NULL)

{

printf("ERROR! Node is null.");

exit(1);

}

Set* temp;

strcpy(set->str, newStr);

set->next = NULL;

if(*head == NULL)

{

set->next = *head;

*head = set;

}

else if(strcmpa((*head)->str, newStr) > 0)

{

set->next = *head;

*head = set;

}

else if(strcmpa((*head)->str, newStr) == 0)

{

printf("That string is already in the set. ");

}

else

{

temp = *head;

while((temp->next != NULL) && (strcmpa(temp->next->str, newStr)) < 0)

temp = temp->next;

set->next = temp->next;

temp->next = set;

}

}

void removeStr(Set **head)

{

char strIn[20];

char rmStr[20];

printf("string: ");

fgets(strIn, 20, stdin);

sscanf(strIn, "%s", &rmStr);

if(strcmpa((*head)->str, rmStr) == 0)

{

Set *temp = *head;

*head = (*head)->next;

free(temp);

return;

}

Set *current = (*head)->next;

Set *prev = *head;

while(current != NULL && prev != NULL)

{

if(strcmpa(current->str, rmStr) == 0)

{

Set *temp = current;

prev->next = current->next;

free(temp);

return;

}

prev = current;

current = current->next;

}

return;

}

Set *uniteSet(Set *set1, Set *set2)

{

Set *mergedSet = (Set *)malloc(sizeof(Set));

if(mergedSet == NULL)

{

printf("ERROR! mergedSet is null.");

exit(1);

}

if(set1 == NULL && set2 == NULL)

return NULL;

if(set1 == NULL)

return set2;

if(set2 == NULL)

return set1;

if(strcmpa(set1->str, set2->str) > 0)

mergedSet = set1;

else

mergedSet = set2;

while(set1 != NULL && set2 != NULL)

{

if(strcmpa(set1->str, set2->str) == 0)

{

mergedSet->next = set1;

set1 = set1->next;

}

else

{

mergedSet->next = set2;

set2 = set2->next;

}

}

if(set1 == NULL)

mergedSet->next = set2;

else

mergedSet->next = set1;

return mergedSet;

}

void intersectSet()

{

}

void symmDiffSet()

{

}

Set *copySet(Set *setSource)

{

if(setSource == NULL)

return NULL;

Set *dest = (Set *)malloc(sizeof(Set));

if(dest == NULL)

{

printf("ERROR! mergedSet is null. ");

exit(1);

}

strcpy(dest->str, setSource->str);

dest->next = copySet(setSource->next);

return dest;

}

void clearSet(Set *head)

{

while(head != NULL)

{

Set *temp = head;

head = temp->next;

temp->next = NULL;

free(temp);

}

}

void printSet(Set *head)

{

printf("{");

while(head != NULL)

{

printf("%s", head->str);

if(head->next != NULL)

printf(", ");

head = head->next;

}

printf("} ");

}

int strcmpa(char *s1, char *s2)

{

   while (*s1 && tolower(*s1) == tolower(*s2))

   {

s1++;

s2++;

   }

   return tolower(*s1) - tolower(*s2);

}