← Back to STL Containers
Question 573
Linked List with Iterator
Linked List with Iterator
Create a classIntList, a singly linked list of ints, with: 1. A private nestedstruct Node { int value; Node* next; };2. A privateNode* head(initiallynullptr) andNode* tail, sopush_backis O(1). 3.IntList()starts empty;~IntList()deletes EVERY node. 4.void push_back(int value): appends a node at the end. 5. A public nested classIteratorholding aNode* cur, with exactly:Iterator(Node* p);int operator*() constreturningcur->value;Iterator& operator++()that movescurtocur->nextand returns*this;bool operator!=(const Iterator& other) const, true when thecurpointers differ. 6.Iterator begin() constonhead, andIterator end() constonnullptr. Your class prints nothing — the hidden test does. Test 1 pushes 10, 20, 30, prints each value followed by a space usingfor (int x : lst), then a newline, thenSum: 60(see Expected Output). Test 2 pushes 7 and 8, walks the list with an explicitIntList::Iteratorloop, and prints7 8on one line thenCount: 2on the next. Do NOT write a main function.
Expected Output:
10 20 30 Sum: 60 7 8 Count: 2
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL ContainersIterators
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.