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

only be added to the end of a stack item of a sta methods for a stack. For full

ID: 3915550 • Letter: O

Question

only be added to the end of a stack item of a sta methods for a stack. For full marks methods gener the array in the stack contains ints. If anv structure, make sure to state them they are data and an integer called manytems (like the Arraybag class) ture which is VERY similar to a bag. Unlike a bag, in a stack order matters.Items can make your methods generic. For partial marks (2 marks each method) assume that you make any assumptions about methods that are available in the stack data nd only the last item of a stack can be removed. Write appropriate push and pop and make sure they are reasonable. Assume that the stack contains an array bop 15 15 enpry stack base 5 base base

Explanation / Answer

CODE:

public class MyStack {
private int maxSize;
private int[] data;
private int top;

public MyStack(int s) {
maxSize = s;
data = new int[maxSize];
top = -1;
}
public void push(int j) {
data[++top] = j;
}
public int pop() {
return data[top--];
}
public int peek() {
return data[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
MyStack theStack = new MyStack(10);
theStack.push(10);
theStack.push(20);
theStack.push(30);
theStack.push(40);
theStack.push(50);
theStack.pop();
  
while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}

Output: