/* listDeletions.c provides functions to delete nodes from a linked list. * @authro Eric Otoo, Wanlin Liu * Version 2.0 of February, 19, 2000 */ #include "list.h" /* deletecurrent(list ls) : deletes the current node of the list * Parmaeters : a pointer to the list. * Pre : the current node cannot be the head * Post : current points to the next node if current is * not the tail, otherwise current points to tail. * Returns : 1 if current is successfully deleted, else 0. */ int deletecurrent(list ls) { /* A pointer to save current, so we can free it later.*/ nodep p; /* If the current node is the dummy node, we cannot delete it.*/ if(list->head == list->current) { printf("Canont delete current, current is the head.\n"); return 0; } /*If the current is the tail, delete it and update tail and current.*/ else if (list->current == list->tail) { list->tail = list->tail->pre; free(list->tail->next); list->tail->next = null; } /*Otherwise, delete and update current.*/ else { p = ls->current; p->pre->next = p->next; p->next-pre = p->pre; advance(ls); free(p); } return 1; }/*deletecurrent(list)*/ /* find_nth_node(list ls, int n) : finds the nth node in a given list. * Parameters : the pointer to the list, a positive * integer indicating the position the node. * Returns : 1 if the node is found, 0 if not * Pre : n cannot exceed the length of the node * Post : current points to the nth node if found * : otherwise, current position is unknown. */ int find_nth_node(list ls, int n){ if (n<1) return 0; reset(ls); while (n--) { /*If the end of the list has not been reached, move forward.*/ if((advance(ls)) == 0) return 0; } return 1; }/* find_nth_node */ /* delete_nth_node(list ls, int n) : deletes the nth node in ls. * Parameters : a pointer to the list, * Pre : n cannot exceed the length of the list. * Returns : 1 if node is successfully deleted, * 0 if the task fails. */ int delete_nth_node(list ls, int n){ /* If the nth node cannot be located, return 0.*/ if (find_nth_node(ls, n) == 0){ printf("Unable to delete.\n"); return 0; } /* If the nth node is found, delete it.*/ else{ deletecurrent(ls); } }/*delete_nth_node(list, int) /* delete_elements(list ls, char[] str) : deletes all occurrences of the * string from the list * Parameters : a pointer to the list and a string * Returns : number of nodes deleted. */ list delete_elements(list ls, char[] str{ int count; /* set the current to the head of the list.*/ reset(ls); /* Go through the list and delete the strings.*/ while(ls->current != ls->tail) { if((strcmp(getcurrent(ls), str)) == 0) { deletecurrent(ls); count++; }/* if */ }/* while */ return count; }/* delete_ element */ /* delete_element takes the pointer to the first node and a string that is to be * deleted and deletes the first occurence of the string from the list. */ int delete_element(list ls, char[] str{ int count; reset(ls); /* Go through the list and delete the strings.*/ while(ls->current != ls->tail) { if((strcmp(getcurrent(ls), str)) == 0) { deletecurrent(ls); count++; return count; }/* if */ }/* while */ return count; }