Question: Complete the global method duplicateElement in the class Quiz which receives a stack and a number and returns a new stack that contains a
Complete the global method " duplicateElement" in the class "Quiz" which receives a stack and a number and returns a new stack that contains a copy of the received stack but the elements that are equal to the received number are duplicated.
Note that:
- You are not allowed to modify or print anything, just complete the implementation of the method.
- You have to keep the order of new stack the same as the original stack.
Example:
Original stack: S = [1, 2, 3, 4, 5] After calling the method with S and 3: New stack = [1, 2, 3, 3, 4, 5]
1 // Credit: 2 // This code was created based on the code provided by GeeksforGeel 3 // https://www.geeksforgeeks.org/stack-data-structure-introduction 4- import java.util.Arrays; 5 public class Stack { 6 static final int MAX = 1000; 7 int top; 8 int a[] = new int[MAX]; // Maximum size of Stack int size = 0; 9 10 11 12 13 14 public boolean isEmpty() { return (top = (MAX - 1)) { System.out.println("Stack Overflow"); return false; } else { a[++top] = x; size++; return true; } } 26 27. 28 29 30 31 32 33 34 35 36 37 38 public int pop() { if (top
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
