Skip to content

Instantly share code, notes, and snippets.

@guyhughes
Created April 19, 2016 18:08
Show Gist options
  • Save guyhughes/6432ab1fac0d6703ca28a8083b1e5627 to your computer and use it in GitHub Desktop.
Save guyhughes/6432ab1fac0d6703ca28a8083b1e5627 to your computer and use it in GitHub Desktop.
public class Cell<E> {
private E value;
public Cell(E value){
if (value == null)
throw new NullPointerException();
this.value = value;
}
public boolean isCell(){return true;}
public static <E> E thing(Class<E> obj){
E a= (E) new Integer(0);
return a;
}
public static void main(String[] args){
Cell<Integer> c = new Cell<Integer>(3);
}
}
import java.util.LinkedList;
//class ObjQueue {
// Object[] elems;
//
// public ObjQueue(int capacity){
// elems = new Object[capacity];
// }
//
// public Object get(int i){
// return elems[i];
// }
//}
public class CircularQueue<E> {
private E[] elems;;
private int front=-1;
private int rear=-1;
@SuppressWarnings("unchecked")
public CircularQueue( int capacity){
elems = (E[]) new Object[capacity];
}
public void rotate(final int n ) {
if (isEmpty()) {
throw new RuntimeException();
}
if (n <= 0) {
throw new IllegalArgumentException("n too small");
}
if (n >= size() ){ // n should be at most size-1 or throw an exception
throw new IllegalArgumentException("n too large");
}
// case 1: if the queue is full
if (previousIndex(front) == rear) {
front -= n;
rear -= n;
return;
}
// case 2: queue is not full
for(int i=0; i < n; ++i){
rear=nextIndex(rear);
elems[rear]=elems[front];
front=nextIndex(front);
}
}
public int size(){
if (front > rear)
return rear - front + 1;
return elems.length - front + rear + 1;
}
public boolean isEmpty(){ return !validFrontAndRear();}
public void enqueue(E e){
final int insert_index=nextIndex(rear);
// is there space to add?
if (insert_index == front){
throw new RuntimeException("queue is full");
}
elems[insert_index] = e;
rear=insert_index;
if( front == -1){
front = insert_index;
}
}
public E dequeue(){
if (!validFrontAndRear()){
throw new RuntimeException("queue is empty");
}
final E e = elems[rear];
elems[rear] = null; // eliminate memory leak
rear = previousIndex(rear);
return e;
}
private boolean validFrontAndRear(){
return front >= 0 && front < elems.length && rear >= 0 && rear < elems.length;
}
private int nextIndex(int i){
return (i+1) % elems.length;
}
private int previousIndex(int i){
return Math.floorMod(i-1,elems.length);
// these two blocks are equivalent
/* int res = (i-1) % elems.length;
if (res >= 0)
return res;
return res + elems.length;*/
}
/* @Override
public String toString(){
final CircularQueue<E> q = new CircularQueue<E>(elems.length);
q.front = this.front;
q.rear = this.rear;
if (!this.validFrontAndRear()){
return "queue is empty";
}
E next = this.dequeue();
final StringBuffer sb = new StringBuffer();
sb.append("[ ");
while(next!= null){
sb.append(next.toString()+" ");
next = this.dequeue();
q.enqueue(next);
}
sb.append(" ]");
while(next!= null){
next = q.dequeue();
this.enqueue(next);
}
return sb.toString();
}*/
@Override
public String toString(){
if(size() == 0)
return "empty";
StringBuffer sb = new StringBuffer();
sb.append("[ ");
for(int i=front; /* cant put condition here because loop will not execute for
rear*/
; i=nextIndex(i)){
sb.append(elems[i].toString()+" ");
if (i == rear)
break;
}
sb.append(" ]");
return sb.toString();
}
public static void main(String[] a){
CircularQueue<String> q = new CircularQueue<String>(29);
for(char c='a'; c <= 'z'; c++){
q.enqueue(""+c);
}
System.out.println(q);
q.rotate(2);
System.out.println(q);
}
}
public class CompilerErrors {
/* public static void main(String[] ar){
}
int f(int x, y ){ // [type] identifer expected
return x + y + z; // cannot find symbol
}
int abc(){
int x=0; // x may not have been initialized
Object y=new Object();
y.toStrings(); // cannot find symbol
if(x == 0)
return -1;
// else// if (x != 0) // missing [unconditional] return statement
return 43;
// return; // missing return value
} */
}
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;
public class Iterators{
public static void main(String[] a){
List<String> l = new ArrayList<String>();
l.add("hi");
l.add("kill");
l.add("the one before me");
l.add("bye");
System.out.println(l+"\n");
Iterator<String> iter = l.iterator();
while(iter.hasNext() ){
final String element = iter.next();
if(element.equals("kill")){
iter.remove();
}
}
System.out.println(l+"\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment