Question
Screenshot of the output is at the bottom. Program provided is as3_Combo_starter.cpp. #include #include #include #include using namespace std; template struct Node { T value;
Screenshot of the output is at the bottom. Program provided is as3_Combo_starter.cpp. #include #include #include #include using namespace std; template struct Node { T value; // The value in this node Node *next; // To point to the next node }; template class Combo { private: int count; Node *head; // List head pointer public: // Constructor Combo() { head = nullptr; count = 0; } // Destructor ~Combo() { Node *p = head; while(p) { delete p; p = p->next; } } void insert(T); // ref. for push_front void remove(T); // ref. for pop_front and pop_back void display() const; // std::string toString(); // here are the new ones T front(); // read front T back(); // read back void push_front(T); void push_back(T); // i.e append void pop_front(); void pop_back(); int size() { return count; } }; template void Combo::insert(T num) { Node *newNode, *p, *prev=nullptr; newNode = new Node; newNode->value = num; newNode->next = nullptr; // if(head == nullptr) if(!head) { // empty list head = newNode; count++; } else { p = head; while( p != nullptr && p->value next; } // p must be pointing to the first bigger, or end if(prev==nullptr) { // the first one is the last one. head = newNode; newNode->next = p; } else { prev->next = newNode; newNode->next = p; } count++; } } template void Combo::remove(T num) { Node *p, *prev=nullptr; if(!head) return; if(head->value == num) { p = head->next; delete head; head = p; count--; } else { p = head; // skip all NOTs while( p != nullptr && p->value != num) { prev = p; p = p->next; } if(p) { prev->next = p->next; delete p; count--; } } } template void Combo::display() const { Node *p = head; while(p->next) { std::cout value next; } std::cout value template std::string Combo::toString() { std::stringstream ss; Node *p = head; bool first = true; while(p){ if (first) first = !first; else ss value; p = p->next; } return ss.str(); } int main() { Combo list; list.push_front(3.3); list.push_front(1.1); list.push_back(5.5); list.push_back(7.7); list.push_back(9.9); auto showd = [](Combo& x) { cout list.pop_front(); list.pop_back(); showd(list); list.pop_front(); list.pop_back(); showd(list); list.pop_front(); list.pop_back(); showd(list); Combo slist; auto shows = [](Combo& x) { cout slist.push_front("fruits"); slist.push_front("delicious"); slist.push_front("eat"); slist.push_back("like"); slist.push_back("apple"); shows(slist); slist.pop_front(); slist.pop_back(); shows(slist); slist.pop_front(); slist.pop_back(); shows(slist); slist.pop_front(); slist.pop_back(); shows(slist); } | |
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