Monday, March 14, 2011

some interesting, useful code

T arr[] = (T []) new Object [n];

int x[] = new int [n];

int front, rear;
front = 0;
rear = 0;

how insert?
arr[rear] = o;
rear++;

how remove?
arr[front] = null;
front++;

eventually, off end of array.
rather, how insert?
arr[rear] = o;
rear = (rear+1) % n;

and same thing for front.

class Person implements Comparable
{
// guts
int compareTo(Object b)
{
Person B = (Person)b;
if (this.age > B.age)
return 1;
else if (this.age < B.age)
return -1;
else
return 0;
}
}

class MyComparator<T> implements Comparator<T>
{
int compare(T a, T b)
{

}
}

main()
{
PriorityQueue<Person> pq = new PriorityQueue<Person> (new MyComparator());
}

class BTree <T extends Comparable> implements Comparable
{
 T element;
 BTree left, right;
 int compareTo(Object o)
 {
BTree<T> bt = (BTree<T>)o;
return this.element.compareTo(bt.element);
 }
}

class LetterAndFreq implements Comparable
{
char letter;
int freq;
int compareTo(Object o)
{
LetterAndFreq b = (LetterAndFreq)o;
if (this.freq > b.freq)
....
}
}

No comments:

Post a Comment