Programming Fundamentals
Download list_contains.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity list_contains
Your task is to add code to this function in list_contains.c:
// Return 1 if value occurs in linked list, 0 otherwise
int contains(char *value, struct node *head) {
// PUT YOUR CODE HERE (change the next line!)
return 42;
}
contains is given two arguments, a string value and head which is the
pointer to the first node in a linked list.
Add code to contains so that it returns 1 if value occurs in the linked
list and otherwise it returns 0.
For example if the linked list contains these 7 elements:
"mozzarella" "pepperoni" "basil" "ham" "tomato bacon" "cheesy-crust" "bocconcini"
and contains is called with value of "mozzarella",
contains should return 1.
Testing
list_contains.c also contains a main function which allows you to test your
contains function.
This main function:
- Asks for how many strings will be in our list,
- reads in and converts that n many strings to a linked list,
- assigns a pointer to the first node in the linked list to
head, - reads another single string from standard input and assigns it to
value. - calls
contains(value, head)and - prints the result.
Do not change this function. If you want to change it, you have misread the question.
Your contains function will be called directly in marking. The main
function is only to let you test your contains function.
Examples
dcc list_contains.c -o list_contains ./list_contains How many strings in initial list?: 4 pepperoni ham basil capsicum Enter word to check contained: basil 1 ./list_contains How many strings in initial list?: 4 pepperoni ham basil capsicum Enter word to check contained: mozzarella 0 ./list_contains How many strings in initial list?: 4 chicken mushroom mushroom pizza-sauce Enter word to check contained: mushroom 1 ./list_contains How many strings in initial list?: 4 tomato bacon capsicum mushroom Enter word to check contained: pepperoni 0 ./list_contains How many strings in initial list?: 0 Enter word to check contained: tomato 0
Assumptions/Restrictions/Clarifications
- String matching is case sensitive.
"Tomato"does not match"tomato". No strings will have thespacecharacter in them containsshould return a single integer.containsshould not change the linked list it is given. Your function should not change the next or data fields of list nodes.containsshould not use arrays.containsshould not call malloc.containsshould not call scanf (or getchar or fgets).containsshould not print anything. It should not call printf.- Do not change the supplied
mainfunction. It will not be tested or marked.