// overstuffed_wrap.c // // Written by YOUR-NAME (YOUR-ZID) // on TODAYS-DATE // // Splits an overstuffed wrap's ingredients into a vegan wrap // and a non-vegan wrap. #include #include #include #include #include // Constants #define MAX_NAME 32 // Provided structs struct ingredient { char name[MAX_NAME]; bool is_vegan; struct ingredient *next; }; struct wrap_split { struct ingredient *vegan; struct ingredient *non_vegan; }; // Function prototypes struct wrap_split *split_wrap(struct ingredient *wrap); struct ingredient *read_wrap(void); void print_wrap(char *label, struct ingredient *wrap); // ----------------------------------------------------------------------------- //////////////// DO NOT CHANGE THE MAIN FUNCTION /////////////////////////////// int main(void) { printf("Enter the ingredients in your wrap:\n"); struct ingredient *wrap = read_wrap(); struct wrap_split *split = split_wrap(wrap); printf("\n"); print_wrap("Vegan wrap", split->vegan); print_wrap("Non-vegan wrap", split->non_vegan); return 0; } // ----------------------------------------------------------------------------- // YOUR FUNCTIONS // ----------------------------------------------------------------------------- // Split the wrap's ingredients into a list of vegan ingredients and a // list of non-vegan ingredients, preserving the original relative order // of each. Return a malloced wrap_split struct holding both lists. struct wrap_split *split_wrap(struct ingredient *wrap) { struct wrap_split *split = malloc(sizeof(struct wrap_split)); split->vegan = NULL; split->non_vegan = NULL; // TODO: Complete this function return split; } // ----------------------------------------------------------------------------- // PROVIDED FUNCTIONS // ----------------------------------------------------------------------------- // DO NOT CHANGE ANY OF THE CODE BELOW HERE // Print a wrap's ingredients void print_wrap(char *label, struct ingredient *wrap) { printf("%s: [", label); struct ingredient *n = wrap; while (n != NULL) { // If you're getting an error here, // you have returned an invalid list printf("%s", n->name); if (n->next != NULL) { printf(", "); } n = n->next; } printf("]\n"); } // Read ingredients from stdin into a linked list struct ingredient *read_wrap(void) { struct ingredient *head = NULL; struct ingredient *tail = NULL; char name[MAX_NAME]; int vegan; while (scanf("%31s %d", name, &vegan) == 2) { struct ingredient *n = malloc(sizeof(struct ingredient)); strcpy(n->name, name); n->is_vegan = vegan; n->next = NULL; if (head == NULL) { head = n; } else { tail->next = n; } tail = n; } return head; }