Question
must be done with while or loop statements and char keys Q9; //Sll.Cpp #Include Node.H #Include ?Iostream? #Include ?Fstream? #Include ?String? #Include ?Cassert? Using Namespace
must be done with while or loop statements and char keys
Q9;
//Sll.Cpp #Include \"Node.H\" #Include ?Iostream? #Include ?Fstream? #Include ?String? #Include ?Cassert? Using Namespace Main_savitch_5; Using
//sll.cpp
#include \"node.h\" #include #include #include #include
using namespace main_savitch_5; using namespace std;
void print_list(node* head) { term t; for (node* cursor = head; cursor != nullptr; cursor = cursor->link()) { t = cursor->data(); cout link() }; };
void eat_white(ifstream& in) /* Pre: \"in\" is a valid input file stream. Post: Any white space skipped over to EOF or the next non-white space character.*/ { while ((in.peek() != EOF) && isspace(in.peek())) in.ignore(); };
int main() { string name; ifstream in; term t; int co; unsigned int exp; node* first1 = nullptr; // pointer to the first node, if any node* last1 = nullptr; // pointer to the last node, if any node* sorted = nullptr;
cout cin >> name; in.open(name); assert(in); while (!in.eof()) { in >> co >> exp;
if (first1 == nullptr) { // TO DO #1: YOU'D NEED A COUPLE OF STATEMENTS HERE. } else { // TO DO #2: YOU'D NEED A COUPLE OF STATEMENTS HERE. };
// TO DO #3: FOR BONUS POINTS, UNCOMMENT THE FOLLOWING STATEMENT // BETWEEN /*...*/ AND IMPLEMENT IT AT THE END OF THE FILE node.cpp /* list_insert_sorted (sorted, term(co,exp)); */
eat_white(in); };
cout print_list(first1);
cout print_list(sorted);
cout return (0); };
//end sll.cpp
//node.cpp
#include \"node.h\" #include // Provides assert #include // Provides NULL and size_t
namespace main_savitch_5 { size_t list_length(const node* head_ptr) // Library facilities used: cstdlib { const node* cursor; size_t answer;
answer = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) ++answer;
return answer; }
void list_head_insert(node*& head_ptr, const node::value_type& entry) { head_ptr = new node(entry, head_ptr); }
void list_insert(node* previous_ptr, const node::value_type& entry) { node* insert_ptr;
insert_ptr = new node(entry, previous_ptr->link()); previous_ptr->set_link(insert_ptr); }
node* list_search(node* head_ptr, const node::value_type& target) // Library facilities used: cstdlib { node* cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) if (target == cursor->data()) return cursor; return NULL; }
const node* list_search(const node* head_ptr, const node::value_type& target) // Library facilities used: cstdlib { const node* cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) if (target == cursor->data()) return cursor; return NULL; }
node* list_locate(node* head_ptr, size_t position) // Library facilities used: cassert, cstdlib { node* cursor; size_t i;
assert(0 cursor = head_ptr; for (i = 1; (i cursor = cursor->link(); return cursor; }
const node* list_locate(const node* head_ptr, size_t position) // Library facilities used: cassert, cstdlib { const node* cursor; size_t i;
assert(0 cursor = head_ptr; for (i = 1; (i cursor = cursor->link(); return cursor; }
void list_head_remove(node*& head_ptr) { node* remove_ptr;
remove_ptr = head_ptr; head_ptr = head_ptr->link(); delete remove_ptr; }
void list_remove(node* previous_ptr) { node* remove_ptr;
remove_ptr = previous_ptr->link(); previous_ptr->set_link(remove_ptr->link()); delete remove_ptr; }
void list_clear(node*& head_ptr) // Library facilities used: cstdlib { while (head_ptr != NULL) list_head_remove(head_ptr); }
void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr) // Library facilities used: cstdlib { head_ptr = NULL; tail_ptr = NULL;
// Handle the case of the empty list. if (source_ptr == NULL) return;
// Make the head node for the newly created list, and put data in it. list_head_insert(head_ptr, source_ptr->data()); tail_ptr = head_ptr;
// Copy the rest of the nodes one at a time, adding at the tail of new list. source_ptr = source_ptr->link(); while (source_ptr != NULL) { list_insert(tail_ptr, source_ptr->data()); tail_ptr = tail_ptr->link(); source_ptr = source_ptr->link(); } }
void list_insert_sorted(node*& head_ptr, const node::value_type& entry) {
// TO DO #3: FOR EXERCISE 5, YOU NEED TO FILL THIS BODY IN. // Depending on how the instructor has set this up, it may be REQUIRED or for BONUS points.
}
}
//end node.cpp
//node.h
#ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include // Provides size_t and NULL
namespace main_savitch_5 { struct term { int coeff; unsigned int exp; term() {}; // only purpose is to facilitate array declaration. term(int c, unsigned int e) : coeff(c), exp(e) {}; char sign() const { return ((coeff bool operator== (const term& t2) const { return (exp == t2.exp); }; bool operator> (const term& t2) const { return (exp > t2.exp); }; bool operator { return (exp }; };
class node { public: // TYPEDEF typedef term value_type;
// CONSTRUCTOR node( const value_type& init_data = value_type(), node* init_link = NULL ) { data_field = init_data; link_field = init_link; }
// Member functions to set the data and link fields: void set_data(const value_type& new_data) { data_field = new_data; } void set_link(node* new_link) { link_field = new_link; }
// Constant member function to retrieve the current data: value_type data() const { return data_field; }
// Two slightly different member functions to retreive // the current link: const node* link() const { return link_field; } node* link() { return link_field; }
private: value_type data_field; node* link_field; };
// FUNCTIONS for the linked list toolkit 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, size_t position); const node* list_locate(const node* head_ptr, 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); void list_insert_sorted(node*& head_ptr, const node::value_type& entry); }
#endif
//end node.h
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started