Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create QeueArrayApp.java for users in hacktober2021 #813

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package packageDA;

public class QeueArrayApp {

public static void main(String[] args) {
// TODO Auto-generated method stub
QueueArray x = new QueueArray(5);
x.display();
x.enqueue(2);
x.enqueue(8);
x.enqueue(3);
x.display();
try {
x.dequeue();
x.display();
x.enqueue(23);
x.enqueue(45);
x.enqueue(59);
x.enqueue(89);
x.display();
x.dequeue();
x.display();
x.enqueue(89);
x.display();
System.out.println("Peek is "+x.peek());
} catch(Exception e) {
System.out.println(e.toString());
}

}

}


class QueueArray{
private int maxSize;
private int[] queArray;
private int front;
private int rear;
private int nItems;

public QueueArray(int S) {
this.queArray = new int[S];
this.maxSize = S;
this.front=0;
this.rear=-1;
this.nItems=0;
}

public boolean isEmpty() {
return this.nItems == 0;
}

public boolean isFull() {
return this.nItems == this.maxSize;
}

public int size() {
return this.nItems;
}

public void enqueue(int value){
if(isFull()){
System.out.println("Queue is full cannot enqueue");
return;
}

/*if(this.rear == this.maxSize -1) {
this.rear =0;
}else {
this.rear++;
}*/

rear = (rear + 1) % maxSize;
this.queArray[rear] = value;
this.nItems++;

}

public int dequeue() throws Exception{
if(isEmpty()){
throw new Exception("Queue is empty cannot remove");
}

int removedItem = this.queArray[front];
/*if(this.front == this.maxSize -1) {
this.front =0;
}else {
front++;
}*/

front = (front + 1) % maxSize;
nItems--;
return removedItem;
}

public int peek() throws Exception{
if(isEmpty()){
throw new Exception("Queue is empty cannot remove");
}
return this.queArray[this.front];
}

public void display(){
if(isEmpty()){
System.out.println("Queue is empty nothing to print");
return;
}

int i = this.front;
while(i != rear) {
System.out.print(this.queArray[i] + " ");
/*if(i == maxSize -1) {
i=0;
} else {
i++;
}*/
i = (i + 1) % maxSize;
}

System.out.print(this.queArray[rear]);
System.out.println();
}
}