Skip to content

Instantly share code, notes, and snippets.

@wahidabd
Created April 27, 2021 15:18
Show Gist options
  • Save wahidabd/833160d86e1a75d0df812bb1b002b307 to your computer and use it in GitHub Desktop.
Save wahidabd/833160d86e1a75d0df812bb1b002b307 to your computer and use it in GitHub Desktop.
public class Queue {
private int size;
char queueArr[];
int front;
int rear;
int currentSize = 0;
public Queue(int size) {
this.size = size;
front = 0;
rear = -1;
queueArr = new char[this.size];
}
public void enqueue(char data) {
if (!isFull()){
rear++;
if (rear == size) {
rear = 0;
}
queueArr[rear] = data;
currentSize++;
}
}
public char dequeue() {
char fr = 0;
if (!isEmpty()){
fr = queueArr[front];
front++;
if (front == size) {
front = 0;
}
currentSize--;
return fr;
}
return fr;
}
public boolean isFull() {
if (currentSize == size) {
return true;
}
return false;
}
public boolean isEmpty() {
if (currentSize == 0) {
return true;
}
return false;
}
public String getString(){
String str = "";
while(!isEmpty()){
str = str + dequeue();
}
return str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment