write c++ statements to do the following: a) set the info of the second node to
ID: 3656788 • Letter: W
Question
write c++ statements to do the following: a) set the info of the second node to 52 b)make the current point to the node with info 10 c) make trail point to the node before temp d) make temp point to an empty list e) set the value of the node before trail to 36 f) write a while loop to make current point to the node with info 10Explanation / Answer
#include namespace vp { template class list { private: struct node { T value; node *next; node *prev; node *operator ++() { if (this->next != NULL) this = this->next; else this = NULL; return this; } node *operator --() { if (this->prev != NULL) this = this->prev; else this = NULL; return this; } T operator *() { if (this == NULL) return NULL; return this->value; } }; node *firstNode; node *lastNode; unsigned int mSize; public: class iterator { private: node *current; public: iterator (node *n) : current(n) {} iterator operator =(iterator n) { this->current = n->current; return *this; } void operator ++() { if (current) current = current->next; } node *operator ->() { return current; } node *operator *() { return current; } bool operator !=(iterator n) { return current != n.current; } }; /** list constructor **/ list() : firstNode(NULL), lastNode(NULL), mSize(0) {} /** list deconstructor **/ ~list() { /** while firstNode isn't NULL **/ while (firstNode != NULL) { /** create new node at firstNode's "next" node **/ node *n = firstNode->next; /** delete firstNode **/ delete firstNode; /** reassign firstNode to the "next" node **/ firstNode = n; } } iterator begin() { return iterator(firstNode); } iterator end() { return iterator(lastNode->next); } unsigned int size() { return mSize; } void push_back(T x) { /** create new node **/ node *n = new node; /** set value equal to x **/ n->value = x; /** since this is the end node there isn't a next node **/ n->next = NULL; /** point to the prev node **/ n->prev = ((lastNode) ? lastNode : NULL); /** if this isn't the first node **/ if (lastNode) /** set the last node's next equal to this one **/ lastNode->next = n; /** make the last node the new node **/ lastNode = n; /** if first node doesn't exist **/ if (!firstNode) /** point to n **/ firstNode = n; mSize ++; } void push_front(T x) { /** create new node **/ node *n = new node; /** set value equal to x **/ n->value = x; /** point to the next node **/ n->next = ((firstNode) ? firstNode : NULL); /** since this is the first node there isn't a prev node **/ n->prev = NULL; /** if this isn't the first node **/ if (firstNode) /** set the first node's prev equal to this one **/ firstNode->prev = n; /** make the first node the new node **/ firstNode = n; /** if last node doesn't exist **/ if (!lastNode) /** point to n **/ lastNode = n; mSize ++; } T pop_back() { /** make sure there is something to pop back **/ if (!size()) return NULL; /** if popping back the last element in the list **/ if (size() == 1) { /** we only need the value **/ T value = lastNode->value; /** delete the node **/ delete lastNode; /** set them equal to NULL again **/ lastNode = firstNode = NULL; /** reduce the size **/ mSize --; /** return the value **/ return value; } /** create a node equal to the 2nd to last node **/ node *n = lastNode->prev; /** next should point to NULL **/ n->next = NULL; /** create a variable equal to the value of the last node **/ T value = lastNode->value; /** delete last node **/ delete lastNode; /** assign last node to the new last node **/ lastNode = n; /** reduce the size **/ mSize --; /** return the value **/ return value; } T pop_front() { /** make sure there is something to pop front **/ if (!size()) return NULL; /** if popping front the last element in the list **/ if (size() == 1) { /** we only need the value **/ T value = firstNode->value; /** delete the node **/ delete firstNode; /** set them equal to NULL again **/ firstNode = lastNode = NULL; /** reduce the size **/ mSize --; /** return the value **/ return value; } /** create a node equal to the 2nd node **/ node *n = firstNode->next; /** prev should point to NULL **/ n->prev = NULL; /** create a variable equal to the value of the first node **/ T value = firstNode->value; /** delete first node **/ delete firstNode; /** assign first node to the new first node **/ firstNode = n; /** reduce the size **/ mSize --; /** return the value **/ return value; } }; } int main() { vp::list myList; vp::list::iterator myIter(myList.begin()); myList.push_back(5); myList.push_back(6); myList.push_back(5); myList.push_back(7); for (vp::list::iterator iter(myList.begin()); iter != myList.end(); ++ iter) std::coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.