/**************************************************************************/ /* File: linked_list.cc */ /**************************************************************************/ /* Project: Continuous Speech Recognition Search Algorithms */ /* Author: Neeraj Deshmukh, Aravind Ganapathiraju */ /* Class: EE 8993 Spring 1996 Date: March 27, 1996 */ /**************************************************************************/ /*======================================================================== This file defines the generic linked list class that is used in various applications in the speech recognizer ========================================================================*/ /*------------------------------------------------------------------------ system and ISIP include files ------------------------------------------------------------------------*/ #include "linked_list.h" /*------------------------------------------------------------------------ forward declaration of classes ------------------------------------------------------------------------*/ class Linked_List; /*======================================================================== Linked_List function definitions for the generic linked list class ========================================================================*/ /*--------------------------- constructors and destructor ---------------------------*/ // default constructor // Linked_List :: Linked_List (void *elem_ptr = NULL) { // create members of the object // next_link_ptr = NULL; prev_link_ptr = NULL; element_ptr = elem_ptr; } // destructor // Linked_List :: ~Linked_List () { // free all memory // delete [] element_ptr; delete next_link_ptr; } /*---------------------- input / output methods ----------------------*/ // get element stored in link // void *Linked_List :: get_element_cc () { // return the element // return (element_ptr); } // set element at current link // void Linked_List :: set_element_cc (void *elem_ptr) { // set the current element // element_ptr = elem_ptr; } /*------------------- linked list methods -------------------*/ // get the next link // Linked_List *Linked_List :: get_next_link_cc () { // return the next link in the list // return (next_link_ptr); } // get the previous link // Linked_List *Linked_List :: get_prev_link_cc () { // return the next link in the list // return (prev_link_ptr); } // add an object to list // Linked_List *Linked_List :: add_link_cc (void *elem_ptr) { // check if this link already exists, do not duplicate it // if (find_link_cc (elem_ptr) != NULL) return (NULL); // create a new link for the given element // Linked_List *new_link_ptr = new Linked_List (elem_ptr); // add this at the head of the list // new_link_ptr->next_link_ptr = next_link_ptr; next_link_ptr->prev_link_ptr = new_link_ptr; next_link_ptr = new_link_ptr; new_link_ptr->prev_link_ptr = this; // return the head of the linked list // return (new_link_ptr); } // find link with given element // Linked_List *Linked_List :: find_link_cc (void *elem_ptr) { // traverse the linked list looking for matching element // for (Linked_List *index_ptr = this; index_ptr != NULL; index_ptr = index_ptr->next_link_ptr) { // check for matching element // if (index_ptr->element_ptr == elem_ptr) { // return link if found match // return (index_ptr); } } // else return NULL // return (NULL); } // delete link with matching element // BOOL Linked_List :: remove_link_cc (void *elem_ptr) { // traverse the linked list looking for matching element // for (Linked_List *index_ptr = this, *next_ptr = next_link_ptr; next_ptr != NULL; index_ptr = index_ptr->next_link_ptr, next_ptr = next_ptr->next_link_ptr) { // check for matching element // if (next_ptr->element_ptr == elem_ptr) { // delete this link and rejoin the modified list // index_ptr->next_link_ptr = next_ptr->next_link_ptr; // delete the matching link and free memory there // next_ptr->next_link_ptr = NULL; delete next_ptr; // return success // return (TRUE); } } // else matching element not found // return (FALSE); }