Question: // FILE: node1.cxx // IMPLEMENTS: The functions of the node class and the // linked list toolkit (see node1.h for documentation). // INVARIANT for the
// FILE: node1.cxx // IMPLEMENTS: The functions of the node class and the // linked list toolkit (see node1.h for documentation). // INVARIANT for the node class: // The data of a node is stored in data_field, and the link in link_field. #include "node1.h" #include
Using the the sample code of linked list node1.h and node1.cpp
Implement the list_piece() function and a driver main program.
Implement a main program that takes a text file as an input (as a command line argument or after a prompt) and processes followings. The words in the input text file should be stored in a linked list, one word at a node. So you have to modify the node1.h to accommodate a string. Then ask the user to pick two words, one for the starting and one for the ending word. And search the original list to find a new sub-list that contains the items, as a starting and ending node, and print out the new list. When you print, include the last word. So you have to implement the list_piece() to include the last word as well. If there's no such sub-list, just print out an error message.
Once you printout the new sub-list, you need to sort the sub-list (in dictionary order) using the insertion sort algorithm and print out the new sorted sub-list. When you sort the list, you must rearrange the link structure of nodes by updating associated pointers, not by copying/moving the strings between nodes.
Just in case, here's a detailed description of the insertion sort algorithm and a sample implementation.
http://www.algolist.net/Algorithms/Sorting/Insertion_sort
The input text file may have sentences with punctuation marks. But to simplify the issue, let's assume that the text file will have only five punctuation marks . , ' - ? . So when you assign a string to a node, you need to get rid of those five punctuation marks and store only the word itself.
// FILE: node1.h // PROVIDES: A class for a node in a linked list, and list manipulation // functions, all within the namespace main_savitch_5 // // TYPEDEF for the node class: // Each node of the list contains a piece of data and a pointer to the // next node. The type of the data is defined as node::value_type in a // typedef statement. The value_type may be any // of the built-in C++ classes (int, char, ...) or a class with a copy // constructor, an assignment operator, and a test for equality (x == y). // // CONSTRUCTOR for the node class: // node( // const value_type& init_data = value_type(), // node* init_link = NULL // ) // Postcondition: The node contains the specified data and link. // NOTE: The default value for the init_data is obtained from the default // constructor of the value_type. In the ANSI/ISO standard, this notation // is also allowed for the built-in types, providing a default value of // zero. The init_link has a default value of NULL. // // NOTE: // Some of the functions have a return value which is a pointer to a node. // Each of these functions comes in two versions: a non-const version (where // the return value is node*) and a const version (where the return value // is const node*). // EXAMPLES: // const node *c; // c->link( ) activates the const version of link // list_search(c,... calls the const version of list_search // node *p; // p->link( ) activates the non-const version of link // list_search(p,... calls the non-const version of list_search // // MEMBER FUNCTIONS for the node class: // void set_data(const value_type& new_data) // Postcondition: The node now contains the specified new data. // // void set_link(node* new_link) // Postcondition: The node now contains the specified new link. // // value_type data( ) const // Postcondition: The return value is the data from this node. // // const node* link( ) const <----- const version // node* link( ) <----------------- non-const version // See the note (above) about the const version and non-const versions: // Postcondition: The return value is the link from this node. // // FUNCTIONS in the linked list toolkit: // size_t list_length(const node* head_ptr) // Precondition: head_ptr is the head pointer of a linked list. // Postcondition: The value returned is the number of nodes in the linked // list. // // void list_head_insert(node*& head_ptr, const node::value_type& entry) // Precondition: head_ptr is the head pointer of a linked list. // Postcondition: A new node containing the given entry has been added at // the head of the linked list; head_ptr now points to the head of the new, // longer linked list. // // void list_insert(node* previous_ptr, const node::value_type& entry) // Precondition: previous_ptr points to a node in a linked list. // Postcondition: A new node containing the given entry has been added // after the node that previous_ptr points to. // // const node* list_search(const node* head_ptr, const node::value_type& target) // node* list_search(node* head_ptr, const node::value_type& target) // See the note (above) about the const version and non-const versions: // Precondition: head_ptr is the head pointer of a linked list. // Postcondition: The pointer returned points to the first node containing // the specified target in its data member. If there is no such node, the // null pointer is returned. // // const node* list_locate(const node* head_ptr, size_t position) // node* list_locate(node* head_ptr, size_t position) // See the note (above) about the const version and non-const versions: // Precondition: head_ptr is the head pointer of a linked list, and // position > 0. // Postcondition: The pointer returned points to the node at the specified // position in the list. (The head node is position 1, the next node is // position 2, and so on). If there is no such position, then the null // pointer is returned. // // void list_head_remove(node*& head_ptr) // Precondition: head_ptr is the head pointer of a linked list, with at // least one node. // Postcondition: The head node has been removed and returned to the heap; // head_ptr is now the head pointer of the new, shorter linked list. // // void list_remove(node* previous_ptr) // Precondition: previous_ptr points to a node in a linked list, and this // is not the tail node of the list. // Postcondition: The node after previous_ptr has been removed from the // linked list. // // void list_clear(node*& head_ptr) // Precondition: head_ptr is the head pointer of a linked list. // Postcondition: All nodes of the list have been returned to the heap, // and the head_ptr is now NULL. // // void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr) // Precondition: source_ptr is the head pointer of a linked list. // Postcondition: head_ptr and tail_ptr are the head and tail pointers for // a new list that contains the same items as the list pointed to by // source_ptr. The original list is unaltered. // void list_piece( // const node* start_ptr, const node* end_ptr, // node*& head_ptr, node*& tail_ptr // ) // Precondition: start_ptr and end_ptr are pointers to nodes on the same // linked list, with the start_ptr node at or before the end_ptr node // Postcondition: head_ptr and tail_ptr are the head and tail pointers for a // new list that contains the items from start_ptr up to but not including // end_ptr. The end_ptr may also be NULL, in which case the new list // contains elements from start_ptr to the end of the list. // // DYNAMIC MEMORY usage by the toolkit: // If there is insufficient dynamic memory, then the following functions throw // bad_alloc: the constructor, list_head_insert, list_insert, list_copy, // list_piece. #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
