Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package its101.mdwk07;
- class Queue {
- private static int front, rear, capacity;
- private static int[] queue;
- Queue(int size) {
- front = rear = 0;
- capacity = size;
- queue = new int[capacity];
- }
- // Create Enqueue() method, this will be use to insert an element into the queue
- static void enqueue(int item) {
- // Check if queue is full
- if (capacity == rear) {
- System.out.println("Error! Queue is overflow.");
- }
- // Insert element at the rear
- else {
- queue[rear] = item;
- rear++;
- }
- }
- // Create Dequeue() method, this will be use to remove an element from the queue
- static void dequeue() {
- // Check if queue is empty
- if (front == rear) {
- System.out.println("Error! Queue is underflow.");
- }
- // Shift elements to the right by one place until rear
- else {
- if (rear - 1 >= 0) System.arraycopy(queue, 1, queue, 0, rear - 1);
- // Set queue[rear] to 0
- if (rear < capacity)
- queue[rear] = 0;
- // Decrement rear
- rear--;
- }
- }
- // Create queuePrint() method, this will be use to print queue elements
- static void queuePrint() {
- int i;
- if (front == rear) {
- System.out.println("Queue is Empty");
- return;
- }
- // Traverse from front to rear and print elements
- for (i = front; i < rear; i++) {
- System.out.printf(" %d ", queue[i]);
- }
- }
- // Create queuePrint() method, this will be use to print the front of queue
- static void peek() {
- if (front == rear) {
- System.out.println("Queue is Empty");
- return;
- }
- System.out.printf("\nFront element of the queue: %d \n", queue[front]);
- }
- // Create isEmpty() method, this will check if the queue is empty
- public boolean isEmpty() {
- return (front == rear);
- }
- // Create isFull() method, this will check if the queue is full
- public boolean isFull() {
- return (rear == capacity);
- }
- }
- public class QueueFinal {
- public static void main(String[] args) {
- // Create a queue of capacity 5
- Queue ralph = new Queue(5);
- // Check if the queue is empty using isEmpty()
- boolean resultEmpty = ralph.isEmpty();
- System.out.println("Is queue empty: " + resultEmpty); // This will print true (queue is empty)
- // Insert elements in the queue using enqueue()
- ralph.enqueue(1);
- ralph.enqueue(2);
- ralph.enqueue(3);
- ralph.enqueue(4);
- ralph.enqueue(5);
- // Print Queue elements again using queuePrint()
- System.out.println("Element of the queue after enqueue operation: ");
- ralph.queuePrint(); // This will now print the new elements inserted to the queue
- // Check again if the queue is empty
- resultEmpty = ralph.isEmpty();
- System.out.println("\nIs queue empty: " + resultEmpty); // This will now print false (queue is not empty)
- // Dequeue elements using dequeue()
- ralph.dequeue();
- ralph.dequeue();
- // Print Queue elements after the dequeue operation
- System.out.println("Element of the queue after two dequeue operations: ");
- ralph.queuePrint();
- // Print the front of the queue using the peek()
- ralph.peek();
- // Check if the queue is full using isFull()
- boolean resultFull = ralph.isFull();
- System.out.println("Is queue full: " + resultFull);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement