Given an iterator
with methods next()
and hasNext()
, create a wrapper iterator, PeekableInterface
, which also implements peek()
. peek
shows the next element that would be returned on next()
.
Example:
Input: 1 2 3
Output:
2 2 1 Not Exist 0
Approach:
C++
#include <bits/stdc++.h>using namespace std;class MyIterator{private:MyIterator *nextIt = NULL;int data;public:MyIterator(MyIterator *nextPtr = NULL){nextIt = nextPtr;}//function to get the dataint getData(){return data;}//funtion to set the datavoid setData(int newData){data = newData;}//function to set the next iteratorvoid setNextIt(MyIterator *nxt){nextIt = nxt;}//function to check for next teratorbool hasNext(){return nextIt != NULL;}//function to get the next valueint next(){return nextIt->getData();}};struct NoNextException : public exception{const char *what() const throw(){return "Not Exist";}};class PeekableInterface : public MyIterator{public:PeekableInterface(PeekableInterface *nextPtr = NULL){this->setNextIt(nextPtr);}int peek(){try{if (this->hasNext())return this->next();elsethrow NoNextException();}catch (NoNextException &e){cout << e.what() << endl;return 0;}}};int main(){PeekableInterface *it = new PeekableInterface();it->setData(1);PeekableInterface *it2 = new PeekableInterface(it);it2->setData(2);PeekableInterface *it3 = new PeekableInterface(it2);it3->setData(3);cout << it3->peek() <<"\n";cout << it3->next() <<"\n";cout << it2->peek() <<"\n";cout << it->peek() <<"\n";return 0;}
No comments:
Post a Comment