(C++) Suppose you want to use a linked list where the items stored in the list are strings from the standard library string class, how would you change the node1.h header file?
Header File:
#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include <cstdlib> // Provides size_t and NULL
namespace main_savitch_5 {
class node {
public:
// TYPEDEF
typedef double value_type;
// CONSTRUCTOR
node(const value_type& init_data=value_type( ), node*
init_link=NULL)
{ data_field = init_data; link_field = init_link; }
// MODIFICATION MEMBER FUNCTIONS
node* link( ) { return link_field; }
void set_data(const value_type& new_data) { data_field =
new_data; } void set_link(node* new_link) { link_field = new_link;
}
// CONST MEMBER FUNCTIONS
value_type data( ) const { return data_field; } const node* link( )
const { return link_field; }
private:
value_type data_field; node *link_field;
};
// FUNCTIONS for the linked-list toolkit
std::size_t list_length(const node* head_ptr);
void list_head_insert(node*& head_ptr, const
node::value_type& entry); void list_insert(node* previous_ptr,
const node::value_type& entry); node* list_search(node*
head_ptr, const node::value_type& target); const node*
list_search
(const node* head_ptr, const node::value_type&
target);
node* list_locate(node* head_ptr, std::size_t position);
const node* list_locate(const node* head_ptr, std::size_t
position); void list_head_remove(node*& head_ptr);
void list_remove(node* previous_ptr);
void list_clear(node*& head_ptr);
void list_copy(const node* source_ptr, node*& head_ptr,
node*& tail_ptr);
} #endif
Here the node1.h will have to specify that the value_type it is holding is of string type instead of the double type.
For Example Here is the changed code:
#include <string>
class node {
public:
// TYPEDEF
typedef string value_type;
// CONSTRUCTOR
node(const value_type& init_data=value_type( ), node*
init_link=NULL)
{ data_field = init_data; link_field = init_link; }
// MODIFICATION MEMBER FUNCTIONS
node* link( ) { return link_field; }
void set_data(const value_type& new_data) { data_field =
new_data; } void set_link(node* new_link) { link_field = new_link;
}
// CONST MEMBER FUNCTIONS
value_type data( ) const { return data_field; } const node* link( )
const { return link_field; }
private:
value_type data_field; node *link_field;
};
Get Answers For Free
Most questions answered within 1 hours.