Wednesday, 14 November 2018

How to implement custom Queue in Java

public class MyQueue {
public static void main(String[] args) {


Queue q = new Queue();
q.enQueue(5);
q.enQueue(8);
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.show();
}
}

class Queue {
int size;
int[] queue;

int front;
int rear;

Queue() {
size = 0;
queue = new int[5];
}

public void enQueue(int data) {
queue[rear] = data;
rear = rear + 1;
size = size + 1;
}

public void show() {
for (int i = 0; i < queue.length; i++) {
System.out.println(queue[i]);
}
}

}

No comments:

Post a Comment