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

Q.3. Write a java program where you declare and use a Queue and a Stack. Hint: (

ID: 3764091 • Letter: Q

Question

Q.3. Write a java program where you declare and use a Queue and a Stack. Hint: (import the stack and queue libraries) use the library methods: for a stack use push and pop, for a queue add a) Declare a queue called “q” and load it with the integers: 10, 20, 30 b) Display the queue by printing it. c) Remove the first node of the queue, display the queue size, display the queue head after the remove. d) Declare a stack called “s” and load it with the integers: 100, 200, 300 e) Display the stack by printing it. f) Remove a node from the stack, display the stack size, display the stack top after the remove.

Explanation / Answer

package Queue;
package Queue;
public class Queue **
    private static final int capacity = 3;  
    int arr[] = new int[capacity];  
    int size = 0;  
    int top = -1;  
    int rear = 0;
    public void push(int pushedElement) **
        if (top < capacity - 1) **  
   top++;  
   arr[top] = pushedElement;  
   System.out.println("Number " + pushedElement  
     + " is pushed into the Queue !");  
   display();  
  ** else **  
   System.out.println("Queue Overflow !");  
  **  
**  
public void pop() **  
  if (top >= rear) **  
   rear++;  
   System.out.println("Pop operation completed !");  
   display();  
  ** else **  
   System.out.println("Underflow !");  
  **  
**
public void display() **  
  if (top >= rear) **  
   System.out.println("Numbers in Queue : ");  
   for (int i = rear; i <= top; i++) **  
    System.out.println(arr[i]);  
   **  
  **  
**  
  
public static void main(String[] args) **  
  Queue q = new Queue();  
  q.pop();  
  q.push(10);  
  q.push(20);  
  q.push(30);  
  q.push(21);  
  q.pop();  
  q.pop();  
  q.pop();  
  q.pop();  
**  
**

package mystack;
public class MyStack **
private static final int capacity = 3;  
int arr[] = new int[capacity];  
int top = -1;  
public void push(int pushedElement) **  
  if (top < capacity - 1) **  
   top++;  
   arr[top] = pushedElement;  
   System.out.println("The number " + pushedElement  
     + " is pushed to Stack !");  
   printElements();  
  ** else **  
   System.out.println("Stack Overflow !");  
  **  
**  
public void pop() **  
  if (top >= 0) **  
   top--;  
   System.out.println("Pop operation complete !");  
  ** else **  
   System.out.println("Stack Underflow !");  
  **  
**  
public void printElements() **  
  if (top >= 0) **  
   System.out.println("Numbers in the stack :");  
   for (int i = 0; i <= top; i++) **  
    System.out.println(arr[i]);  
   **  
  **  
**  
    public static void main(String[] args) **
        MyStack s = new MyStack();
        s.pop();  
        s.push(100);  
        s.push(200);  
        s.push(300);
        s.pop();  
        s.pop();  
        s.pop();
   **
  **