// CSCI 2720 Data Structures Spring 2004 // // PtrToPtrToNode // // Contains sample code for a private helper insert function which is part of an // ordered linked list of integers, in order to illustrate the use of the // Node** type. This is close in spirit to the "locative" data type used by // Lewis and Denenberg in their text. // ----------- one function implementation from the ***.cc file below ---------- // iterative insert -- private int OrdList::Insert(int new_value, Node * &T) { Node **P = &T; // start by pointing to the input pointer while ( *P && new_value < (*P)->Value) // advance along list P = &((*P)->Next); if ( *P && new_value == (*P)->Value) // new_value already in list return 0; (*P) = new Node(new_value, *P); // insert new_value into the list, // locating it in a new Node if ( !(*P) ) error("out of space for Insert"); // defined elsewhere; prints error // message to cerr and calls exit( ) to end the // program in case memory was not available for new Node return 1; // new_value successfully inserted into list } // end of Insert(int, Node*&) // Notice that using Node** P, insertion at the beginning and end do not // require special cases in the code. The work is performed by the private // helper function Insert(int, Node*&) so that the public member function // Insert(int) can call it with a reference to the head of the list, which // should be kept as protected or private in the class OrdList.