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:

  1. Asks for how many strings will be in our list,
  2. reads in and converts that n many strings to a linked list,
  3. assigns a pointer to the first node in the linked list to head,
  4. reads another single string from standard input and assigns it to value.
  5. calls contains(value, head) and
  6. 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