Programming Fundamentals

Download list_insert_tail.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity list_insert_tail

Your task is to add code to this function in list_insert_tail.c:

// Insert a new node containing value at the end of the linked list.
// Parameters:
//      `int value`         : The value to insert.
//      `struct list *list` : a struct * containing the head pointer of the 
//      linked list.
void insert_tail(int value, struct list *list) {
    // PUT YOUR CODE HERE
}

insert_tail is given two arguments:

Add code to insert_tail so that it creates a new list node (using malloc) containing value and places it at the end of the list.

insert_tail should return nothing.

For example if value is 12 and the linked list contains these 3 elements:

16, 7, 8

insert_tail should modify the linked list so that it now has these elements:

16, 7, 8, 12

Testing

list_insert_tail.c also contains a main function which allows you to test your insert_tail function.

This main function:

  1. Asks for the size of the initial linked list
  2. converts the first set of scanned inputs to a linked list
  3. stores the first node of the linked list in a struct list.
  4. reads a single integer from standard input and assigns it to value
  5. calls insert_tail(value, list)
  6. prints the result.

Do not change this main function. If you want to change it, you have misread the question.

Your insert_tail function will be called directly in marking. The main function is only to let you test your insert_tail function

Examples

dcc list_insert_tail.c -o list_insert_tail
./list_insert_tail
How many numbers in initial list?: 3
16 7 8
Enter value to insert: 12
[16, 7, 8, 12]
./list_insert_tail
How many numbers in initial list?: 1
16
Enter value to insert: 42
[16, 42]
./list_insert_tail
How many numbers in initial list?: 0
Enter value to insert: 2
[2]

Assumptions/Restrictions/Clarifications