Question: JAVA Implement following code using linked list: import java.util.Scanner; class Queue { int PPS[], front, rear, size, length; public Queue(int n) { // Constructor size

JAVA

Implement following code using linked list:

import java.util.Scanner;

class Queue { int PPS[], front, rear, size, length;

public Queue(int n) { // Constructor size = n; length = 0; PPS = new int[size]; front = rear = -1; }

public boolean isFull() { // If full or not if (front == 0 && rear == (size - 1)) return true; else return false; }

public boolean isEmpty() { // If empty or not if (length == 0) return true; else return false; }

public int getLength() { return length; }

public void enqueue(int pps) { if (rear == -1) { // If queue is empty rear = 0; PPS[rear] = pps; front = 0; length++; } else if ((rear + 1) < size) // If queue is not empty and have space { PPS[++rear] = pps; length++; } else if (isFull()) System.out.println("Queue is Full."); }

public void dequeue() { if (isEmpty()) { System.out.println("Queue is empty."); } else { int deleted = PPS[front]; if (front == rear) { // If only one element is left front = rear = -1; } else front++; length--; System.out.println("Deleted element: " + deleted); } }

public void print() { if (isEmpty()) { System.out.println("Queue is Empty."); } else { System.out.println(" Queue:"); int index; for (index = front; index <= rear; index++) { System.out.print(PPS[index] + " "); } System.out.println(); } } }

public class Welfare { private static Scanner stream;

public static void main(String args[]) { int capacity, ch; stream = new Scanner(System.in); System.out.print("Enter queue capacity: "); capacity = stream.nextInt(); Queue queue = new Queue(capacity); // Create queue do { System.out.println(" Queue Options:"); System.out.println("1. Enqueue."); System.out.println("2. Dequeue."); System.out.println("3. Check for full."); System.out.println("4. Check for empty."); System.out.println("5. Get queue size."); System.out.println("6. Exit."); System.out.print(" Choose an option: "); ch = stream.nextInt(); // read user's choice switch (ch) { case 1: System.out.print("Enter a PPS to enqueue: "); queue.enqueue(stream.nextInt()); break; case 2: queue.dequeue(); break; case 3: if (queue.isFull()) System.out.println("Queue is full."); else System.out.println("Queue is not full."); break; case 4: if (queue.isEmpty()) System.out.println("Queue is empty."); else System.out.println("Queue is not empty."); break; case 5: System.out.println("Queue size: " + queue.getLength()); break; case 6: break; default: System.out.println("Wrong choice. "); break; } queue.print(); } while (ch != 6); // Exit on user's choice } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!