Skip to content

Instantly share code, notes, and snippets.

@omkz
Created November 26, 2011 10:11
Show Gist options
  • Save omkz/1395414 to your computer and use it in GitHub Desktop.
Save omkz/1395414 to your computer and use it in GitHub Desktop.
java queue
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.omenk;
/**
*
* @author omenkzz
*/
public class QueueTest {
private int[] queue;
int currIndex = 0;
public QueueTest(int n) {
queue = new int[n];
}
/**
* increase antrian
*/
private void incrementSize() {
int[] naik = new int[queue.length + 1];
for (int i = 0; i < queue.length; i++) {
naik[i] = queue[i];
}
queue = naik;
}
/**
* tambah data ke antrian
*/
public void enquee(int i) {
if (currIndex >= queue.length) {
incrementSize();
}
queue[currIndex] = i;
currIndex++;
System.out.println("enquee: " + i);
}
/**
* move the queue up
*/
public void moveUp() {
for (int x = 0; x < queue.length - 1; x++) {
queue[x] = queue[x + 1];
}
}
// remove antrian
public int dequee() {
int r = queue[0];
moveUp();
currIndex--;
System.out.println("removing: quee:" + r);
return r;
}
/**
* menampilkan data antrian
*/
public void display() {
System.out.println("========= display ========== ");
for (int i = 0; i < queue.length; i++) {
System.out.println("[" + i + "] = " + queue[i]);
}
System.out.println("=============================");
}
public void preCondition() {
System.out.println("========= preCondition =========");
display();
}
public void postCondition() {
System.out.println(" ========= postCondition =========");
display();
}
public void process() {
System.out.println("========= process =========");
enquee(1);
enquee(2);
enquee(3);
enquee(4);
enquee(5);
dequee();
dequee();
enquee(6);
enquee(7);
}
public static void main(String[] args) {
int size = 5;
if (args.length > 0) {
size = Integer.parseInt(args[0]);
}
QueueTest q = new QueueTest(size);
q.preCondition();
q.process();
q.postCondition();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment