#include "list.h" /* Inserts number at the beginning of list L. L points to * the first element of the list - a dummy cell storing the * number of cells in the list. */ void insert_list(int number, List L) { List e; // allocate space for a cell and store number in it e = (List) malloc(sizeof(Cell)); e->element = number; // insert the cell at teh beginning of L e->next = L->next; L->next = e; (L->element)++; // increment the counter in dummy cell } /* Searches for occurance of number in list L. Returns a pointer * to thecell just before the cell containing number, if it exists. * Returns NULL otherwise. */ List search_list(int number, List L) { if (L->next == NULL) // number does not exists in the list return NULL; if ((L->next)->element == number) // found it! return L; // number is not in the next cell. Search remaining list for it return search_list(number, L->next); } /* Deletes the cell containing number in the list L, if it exists. * Returns -1 otherwise. */ int delete_list(int number, List L) { List x, y; x = search_list(number, L); // look for number if (x == NULL) // number does not exist return -1; // Number is present in the next cell y y = x->next; // remove y x->next = y->next; free(y); // Update the counter (L->element)--; return 1; } /* Prints the elements of list L */ void print_list(List L) { printf("List has %d numbers\n", L->element); for (L = L->next; L != NULL; L = L->next) printf("%d ", L->element); printf("\n"); }