Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Read over the code carefully, noting what each part does. Implement each place that says fill in here. Get your program compiled and running. Run
- Read over the code carefully, noting what each part does.
- Implement each place that says fill in here.
- Get your program compiled and running.
- Run your code and test it! The expected output is at the bottom.
#includeusing namespace std; template struct ListNode { ListNode(T const &x, ListNode *_next=nullptr) : data(x), next(_next) {} ~ListNode() { delete next; } T data; ListNode *next; }; template struct ListIterator { ListIterator(ListNode *_current=nullptr) { /* fill in here */ } ListNode *current; ListIterator &operator++() { /* fill in here */ return *this; } T const &operator*() const { return /* fill in here */ } bool operator!=(ListIterator const &x) const { return /* fill in here */ } }; template struct LinkedList { LinkedList() : front(nullptr), back(nullptr) {} ~LinkedList() { delete front; } ListNode *front, *back; void add_back(T const &x) { /* fill in here -- be careful on the first "add" to update both "front" and "back" */ } void add_front(T const &x) { /* fill in here -- be careful on the first "add" to update both "front" and "back" */ } ListIterator begin() { return /* fill in here */ } ListIterator end() { return /* fill in here */ } }; int main() { LinkedList ll; // add a bunch of stuff for (int i = 0; i < 5; ++i) { ll.add_front(i * 3); ll.add_back(i * 7 + 1); } // print everything using the iterator interface for (ListIterator i = ll.begin(); i != ll.end(); ++i) { cout << *i << " "; } cout << endl; // expected output: "12 9 6 3 0 1 8 15 22 29 " return 0; }
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