Created
February 25, 2010 03:38
-
-
Save jakswa/314205 to your computer and use it in GitHub Desktop.
circular queue in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class QueueJakes { | |
public static void main (String [] args) { | |
Scanner myscan = new Scanner(System.in); | |
System.out.print("Enter max queue size: "); | |
int qSize = myscan.nextInt(), choice; | |
Queue q = new Queue(qSize); | |
do { | |
q.print(); | |
System.out.print("Enter a choice (zero stops):\n 1. Dequeue\n 2. Enqueue\n\nChoose: "); | |
choice = myscan.nextInt(); | |
switch(choice) { | |
case 1: q.dequeue(); break; | |
case 2: q.enqueue((int)(Math.random()*100)); break;//enqueue random int | |
} | |
} while (choice != 0); | |
} | |
static class Queue { | |
int front, rear; | |
int [] Q; | |
public Queue(int size) { | |
/* | |
* -=-=| JACOB IS A DUMB SHIT NOTE |=-=- | |
* 'size+1' because one spot is always kept empty (the 'front' index) | |
* otherwise, "empty" and "full" conditions will match, | |
* and mayhem will ensue. Mayhem was upon me, before I realized this. | |
* there are other ways to do avoid this: | |
* http://en.wikipedia.org/wiki/Circular_buffer#Difficulties | |
*/ | |
Q = new int[size+1]; | |
front = 0; | |
rear = 0; | |
} | |
public void enqueue(int val) { | |
rear = (rear + 1) % Q.length; | |
if (rear == front) { //if full | |
System.out.println("Queue is full."); | |
if (rear == 0) rear = Q.length - 1; else rear = rear - 1; | |
return; | |
} | |
Q[rear] = val; | |
} | |
public int dequeue() { | |
if (rear == front) {//if empty | |
System.out.println("Queue is empty."); | |
return -1; | |
} | |
int tmp = Q[front]; | |
front = (front+1) % Q.length; | |
return tmp; | |
} | |
public void print() { | |
int curr = front; | |
System.out.print("Queue state: "); | |
if (curr == rear) { System.out.print("[empty]"); } | |
else while (curr != rear) { | |
curr = (curr + 1) % Q.length; | |
System.out.print(Q[curr] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks alot jacob!
The fact you included a print algorithm to traverse the Queue was a huge plus!