You are viewing this question as it will appear in the manual grading interface. Return to the normal view when you are done.

Autograder demo: Remove linked list element in C (Check Framework)

Assume that a linked list data structure has been defined in list.h as follows:

1
2
3
4
5
6
7
8
struct node {
  int value;
  struct node *next;
};

struct list {
  struct node *head;
};

Assume that for a list l, the element l->head points to the first element of the list.

Implement the following function that deletes the first element of the list. All other elements of the list should remain in the list in their original order. The function must return the value of the element that was deleted. The element deleted from the list must be properly de-allocated. If the list is originally empty, the function should return -1, and should not change the list (i.e., it must remain empty).

Caution: be extra careful not to use values after they have been freed.

Note: you don't need to write the main function.

deletefirst.c

Correct answer

Click to reveal
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdlib.h>
#include "list.h"

int list_delete_first(struct list *list) {

  if (!list->head) return -1;
  
  int old_value = list->head->value;
  struct node *old_node = list->head;
  list->head = old_node->next;
  free(old_node);
  return old_value;
}

Student view placeholder

In student views this area is used for assessment and score info.
Tools

Staff information

Question

Title:
Autograder demo: Remove linked list element in C (Check Framework)

Variant

Started at:
2026-09-19 10:15:59 (CDT)
Duration:
0s
Show/Hide answer
{}
History
dec frac
rad deg