Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I hope to answer in java Write a java program where you declare and use a Queue

ID: 3763366 • Letter: I

Question

I hope to answer in java

Write a java program where you declare and use a Queue and a Stack. use the library methods: for a stack use push and pop, for a queue add Declare a queue called "q" and load it with the integers: 10, 20, 30 Display the queue by printing it. Remove the first node of the queue, display the queue size, display the queue head after the remove. Declare a stack called "s" and load it with the integers: 100, 200, 300 Display the stack by printing it. Remove a node from the stack, display the stack size, display the stack top after the remove.

Explanation / Answer

Stack Operations using Stack Library Class :

As per problem Statement this below program will push the elements into the statck and display the elements of stack and pop the top the element from the stack.

import java.util.*;

// Declaring user defined Stack Class with Push and Pop methods.
public class StackExampleClass {

// This method will push the elements into the stack and show the elements in the stack using Stack Library Class object called S
static void showpush(Stack st, int a) {
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}

// This method will pop the elements from the stack and show the present elements in the stack using Stack Library Class object called S   
static void showpop(Stack st) {
System.out.print("pop -> ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}

// Main Method have the Stack Class object
public static void main(String args[]) {
Stack s = new Stack();
System.out.println("stack: " + s);
showpush(s, 100); // Inserting Element 100 into the Stack
showpush(s, 200); // Inserting Element 200 into the Stack
showpush(s, 300); // Inserting Element 300 into the Stack
   System.out.println("Stack Size is: "+s.size());
try {
showpop(s); // Pop the the top of the element from the stack and display the remaning elements
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}

Queue Example:

This below Queue Example will insert the elements into the queue and disply the all the elements from the queue and count the number elements from the queue and Remove the element from the queue and show the top of the element of queue. see the below code for Queue Class

import java.util.*;

public class QueueExample {
static String newLine = System.getProperty("line.separator");
public static void main(String[] args) {
  
System.out.println("Adding items to the Queue");
//Creating queue would require you to create instance of Queue.
   //creating an instance of Queue as it is abstract
Queue queue = new LinkedList();
  
//Add elements to queue using add method
queue.add(10);
queue.add(20);
queue.add(30);
  
   // Display the Items from the Queue  
   for(Object object : queue) {
int element = (int) object;
   System.out.println("Items in the queue " + element);}

//Remove the element from the Queue.
System.out.println("remove element: " + queue.remove());
  
   //This would Display the head element of the queue.
System.out.println("Head element: " + queue.peek());
  
   //This would Display the head element of the queue.
System.out.println("Queue Total elements: " + queue.size());
}
}