#include #include struct node { int value; struct node *next; }; struct node *add_to_front(struct node *list, int v); struct node *remove_from_back(struct node *list); void print_list(struct node *list); int main() { struct node *front = NULL; int value; while (1) { printf("Enter a number: "); int rtn = scanf("%d", &value); if (rtn != 1) break; else front = add_to_front(front, value); } print_list(front); front = remove_from_back(front); print_list(front); front = remove_from_back(front); print_list(front); front = remove_from_back(front); print_list(front); return 0; } struct node *add_to_front(struct node *list, int v) { struct node *new_node = malloc(sizeof(struct node)); if (new_node == NULL) { printf("Error: malloc failed in add_to_front\n"); exit(EXIT_FAILURE); } new_node->value = v; new_node->next = list; return new_node; } struct node *remove_from_back(struct node *list) { if (list == NULL) return list; //empty list struct node *cur, *prev; for (cur = list, prev = NULL; cur->next != NULL; prev = cur, cur = cur->next) ; if (prev == NULL) //list has only one node list = list->next; else prev->next = cur->next; //point to NULL free(cur); return list; } void print_list(struct node *list) { for (; list != NULL; list = list->next) { printf("%d ", list->value); } printf("\n"); }