COMPLETE THIS CODE(DONT CHANGE)
package myqueue_J;
public class MyQueue_J {
public int data;
public MyQueue_J next;
public static MyQueue_J front=null;
public static MyQueue_J rear=null;
public static MyQueue_J temp=null;
public MyQueue_J(int d, MyQueue_J n) {
data=d;
next=n;
}
public static void enqueue(int d){
MyQueue_J temp = new MyQueue_J(d, null);
Insert to perform enqueue operation
}
public static void dequeue(){
Insert to perform dequeue operation
}
public static int printQueue(){
int ctr=0;
Insert to display the elements in the queue
}
public static void main(String[] args) {
Insert to perform the desired output
}
}
THE OUTPUT
After the Enqueue operations:
1. Element Front: 10
2. Element rear: 30
3. Elements in the Queue: 10 15 20 25 30
4. Number of Elements in the Queue: 5
The dequeued element is 10.
The dequeued element is 15.
After the Dequeue operations:
1. Element Front: 20
2. Element rear: 30
3. Elements in the Queue: 20 25 30
4. Number of Elements in the Queue: 3
package myqueue_J;
public class MyQueue_J {
public int data;
public MyQueue_J next;
public static MyQueue_J front = null;
public static MyQueue_J rear = null;
public static MyQueue_J temp = null;
public MyQueue_J(int d, MyQueue_J n) {
data = d;
next = n;
}
public static void enqueue(int d) {
MyQueue_J temp = new MyQueue_J(d, null);
if (rear == null) {
front = temp;
} else {
rear.next = temp;
}
rear = temp;
}
public static void dequeue() {
if (front == null) {
System.out.println("Empty");
} else {
System.out.println("The dequeued element is " + front.data + ".");
if (front.next == null) {
rear = null;
}
front = front.next;
}
}
public static int printQueue() {
int ctr = 0;
System.out.print("3. Elements in the Queue: ");
for (temp = front; temp != null; temp = temp.next, ctr++) {
System.out.print(temp.data + " ");
}
System.out.println();
return ctr;
}
public static void main(String[] args) {
MyQueue_J.enqueue(10);
MyQueue_J.enqueue(15);
MyQueue_J.enqueue(20);
MyQueue_J.enqueue(25);
MyQueue_J.enqueue(30);
System.out.println("After the Enqueue operations:");
System.out.println("1. Element Front: " + MyQueue_J.front.data);
System.out.println("2. Element rear: " + MyQueue_J.rear.data);
System.out.println("4. Number of Elements in the Queue: " + printQueue());
System.out.println();
MyQueue_J.dequeue();
MyQueue_J.dequeue();
System.out.println("After the Dequeue operations:");
System.out.println("1. Element Front: " + MyQueue_J.front.data);
System.out.println("2. Element rear: " + MyQueue_J.rear.data);
System.out.println("4. Number of Elements in the Queue: " + printQueue());
System.out.println();
}
}
Comments
Leave a comment