Tuesday, October 25, 2011
HW: modify your Stack to make it Iterable.
class MyStack<T> implements Iterable
{
Iterator<T> iterator()
{
return new MyIterator<T>(this);
}
}
class MyIterator<T> implements Iterator
{
// a copy of my Stack array
T arr;
int curEl = -1;
MyIterator(MyStack<T> ms)
{
arr = new T[ms.top]
for loop to copy ms.arr into this.arr
}
T next()
{
return arr[++curEl];
}
boolean hasNext()
{
return curEl == arr.length;
}
}
This code is probably buggy
LLast part of this data structure.
Moving on: Implement Huffman tree encoding
we will need MANY interacting data structures
binary trees, priority queues, interfaces like comparator and comparable, etc.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment