题目:
题解:
bool flag;
struct PeekingIterator {
struct Iterator *iterator;
int nextElement;
};
struct PeekingIterator* Constructor(struct Iterator* iter) {
struct PeekingIterator* piter = malloc(sizeof(struct PeekingIterator));
piter->iterator = iter;
flag = piter->iterator->hasNext();
if(flag)
{
piter->nextElement = piter->iterator->next();
}
return piter;
}
int peek(struct PeekingIterator* obj) {
return obj->nextElement;
}
int next(struct PeekingIterator* obj) {
int ret = obj->nextElement;
if(flag)
{
flag = obj->iterator->hasNext();
obj->nextElement = obj->iterator->next();
}
return ret;
}
bool hasNext(struct PeekingIterator* obj) {
return flag;
}