#include #include struct node { struct node *prev; int value; struct node *next; }; struct node *front; struct node *back; void add_to_front(int v); void remove_from_back(); void print_list(); int main() { int value; while (1) { printf("Enter a number: "); int rtn = scanf("%d", &value); if (rtn != 1) break; else add_to_front(value); } print_list(); remove_from_back(); print_list(); remove_from_back(); print_list(); remove_from_back(); print_list(); return 0; } void add_to_front(int v) { struct node *new_node; new_node = (struct 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; if (front == NULL) { //special case, empty list front = back = new_node; } else { new_node->next = front; front->prev = new_node; //update prev pointer front = new_node; } } void remove_from_back() { if (back == NULL) return; //list is empty if (back->prev == NULL) { //special case, one node free(back); front = back = NULL; } else { struct node *to_free = back; back = to_free->prev; to_free->prev = NULL; back->next = NULL; free(to_free); } } void print_list() { struct node *cur; for (cur = front; cur != NULL; cur = cur->next) { printf("%d ", cur->value); } printf("\n"); }