// file: example_00.cc // // local include files // #include "Example.h" //------------------ // // implementations for class Node // //------------------ // method: default constructor // Node::Node() { // initialize variables // data_d = (char*)NULL; next_d = (Node*)NULL; // exit gracefully // } // method: destructor // Node::~Node() { // do some bookkeeping // Node* tmp = next_d; // exit gracefully // } // method: set_data // void Node::set_data(char* data_a) { // allocate space // data_d = new char[strlen(data_a) + 1]; strcpy(data_d, data_a); // exit gracefully // } // method: set_next // void Node::set_next(Node* next_a) { // link the pointers // next_d = next_a; // exit gracefully // } // method: get_data // // Note that this method is dangerous because it is returning // a pointer to data internal to a class. // char* Node::get_data() { return (char*)data_d; } // method: get_next // // Note that this method is dangerous because it is returning // a pointer to data internal to a class. // Node* Node::get_next() { return next_d; } //------------------ // // implementations for class List // //------------------ // method: default constructor // List::List() { // initialize variables // head_d = (Node*)NULL; // exit gracefully // } // method: destructor // List::~List() { // do some bookkeeping // Node* tmp = head_d; // exit gracefully // } // method: append // void List::append(char* data_a) { // Create a new node // Node* new_node_ptr = new Node(); new_node_ptr->set_data(data_a); new_node_ptr->set_next((Node*)NULL); // Create a temp pointer // Node *tmp = head_d; // if the list is not null, go to the end of the list // if (tmp != NULL) { // recurse to the end of the list // while (tmp->get_next() != (Node*)NULL) { tmp = tmp->get_next(); } // point the last node to the new node // tmp->set_next(new_node_ptr); } // else, if the list is null, create the first node // else { // make the head of the list the new node // head_d = new_node_ptr; } // exit gracefully // } // method: debug // void List::debug(FILE* fp_a) { // print an informational message // fprintf(fp_a, "... debugging list ...\n"); // set up a temporary pointer that we can use // to recurse over the list // Node *tmp = head_d; // if the list is empty, return // if (tmp == (Node*)NULL) { fprintf(fp_a, "warning: the list is empty\n"); return; } // else: loop over the list // while (tmp != (Node*)NULL) { // display the node // fprintf(fp_a, "location = %lu\n", (void*)tmp); fprintf(fp_a, " the string is stored at = %lu\n", (void*)tmp->get_data()); fprintf(fp_a, " the value of the string = [%s]\n", tmp->get_data()); // advance the list // tmp = tmp->get_next(); } // exit gracefully // fprintf(fp_a, "... end of debugging list ...\n"); } // method: get_head // Node* List::get_head() { return (Node*)head_d; }