/********************************************************************* C201 Computer Programming II Fall 2006 Dana Vrajitoru & The data structure storing linked lists. **********************************************************************/ #include using namespace std; #include #include "list.h" // List traversal function. It prints all the elements of the list. void Output_list(Node_ptr front) { if (!front) // if (front == NULL) cout << "The list is empty" << endl; else { Node_ptr p = front; while (p) { // Output the content of the node and a space. cout << p->word << ' '; // Move the pointer to the next node. p = p->next; } // Output an endl at the end of the list. cout << endl; } } // Returns the number of nodes in the list. int Count_nodes(Node_ptr front) { Node_ptr p = front; int count = 0; while (p != NULL) { ++count; p = p->next; } return count; } // Inserts a new node at the front of the list. The front is a // reference parameter because the new node becomes the front of the // list. void Insert_front(Node_ptr &front, string s) { Node_ptr n = new Node; n->word = s; n->next = front; front = n; } // Removes the front node of the list. If the list is empty, it // returns false. bool Remove_front(Node_ptr &front) { if (!front) return false; else { Node_ptr temp = front; front = front->next; delete temp; return true; } } // Deletes the entire list void Delete_list(Node_ptr &front) { Node_ptr p = front; while (front) { front = front->next; delete p; p = front; } } // Returns a pointer to the last node in the list. It's a special list // traversal that must stop before the pointer scanning the list // becomes NULL. Node_ptr Last_node(Node_ptr front) { if (!front) return NULL; else { Node_ptr last = front; while (last->next != NULL) last = last->next; return last; } } // Searches for a particular string in the list. If the string is // there, it returns a pointer to the node that contains it. If not, // it return NULL. Node_ptr Search(Node_ptr front, string s) { if (!front) return NULL; else { Node_ptr loc = front; while (loc != NULL) { if (loc->word == s) return loc; loc = loc->next; } return NULL; } } //****************** Homework functions ****************** // Insert the string as a new node at the back of the list. void Insert_back(Node_ptr &front, string s) { // To be implemented by the students. } // Returns a string obtained by the concatenation of the words in all // the nodes of the list in order and separated by spaces. string Concatenate(Node_ptr front) { // To be implemented by the student. return ""; } // Returns a pointer to the node number i in the list, where the first // node is at position 0. If the list has less than i+1 nodes, it // returns NULL. Node_ptr Access_index(Node_ptr front, int i) { // To be implemented by the students. return NULL; } // Converts the first letter of every word in the list to uppercase. void Toupper_all(Node_ptr front) { // To be implemented by the students. } // Converts the first letter of every word in the list to lowercase. void Tolower_all(Node_ptr front) { // To be implemented by the students. } // Reverses the list. It will most likely create a new list containing // the same nodes as the list starting from front but in reverse // order, then delete the old list and store the address of the first // element of the new list in the pointer front. void Reverse_list(Node_ptr &front) { // To be implemented by the students. }