aboutsummaryrefslogtreecommitdiff
path: root/peeking_iterator.cpp
blob: 1c4d0b9b3eb0f6d99579f96ce2fef700e9a4f072 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "leetcode.h"

class Iterator {
  struct Data;
  Data *data;

public:
  Iterator(const vector<int> &nums);
  Iterator(const Iterator &iter);
  int next();
  bool hasNext() const;
};

class PeekingIterator : public Iterator {
public:
  int nextVal;
  PeekingIterator(const vector<int> &nums) : Iterator(nums) {
    nextVal = Iterator::next();
  }
  int peek() { return nextVal; }
  int next() {
    int tmp = nextVal;
    if (Iterator::hasNext())
      nextVal = Iterator::next();
    else
      nextVal = NULL;
    return tmp;
  }
  bool hasNext() const { return (nextVal != NULL); }
};

int main() {}