In: Computer Science
Complete the following functions in the Stack.java/Stack.h files
(ATTACHED BELOW):
a. void push(int val)
b. int pop()
c. int getSize()
public class Stack {
private int maxStackSize, topOfStack;
private int[] stack;
public Stack(int maxStackSize) {
if (maxStackSize <= 0)
System.out.println("Stack size should be a positive
integer.");
else {
this.maxStackSize = maxStackSize;
topOfStack = -1;
stack = new int[maxStackSize];
}
}
public void push(int val) { // complete this function
}
public int pop() { // complete this function
}
public int getSize() { // complete this function
}
}
public class Stack {
private int maxStackSize, topOfStack;
private int[] stack;
public Stack(int maxStackSize) {
if (maxStackSize <= 0)
System.out.println("Stack size should be a positive integer.");
else {
this.maxStackSize = maxStackSize;
topOfStack = -1;
stack = new int[maxStackSize];
}
}
public void push(int val) { // complete this function
if (topOfStack >= maxStackSize - 1)
System.out.println("Stack is already full.");
else
stack[++topOfStack] = val;
}
public int pop() { // complete this function
if (topOfStack == -1) {
System.out.println("Stack is already empty");
return -1;
}
else
return stack[topOfStack--];
}
public int getSize() { // complete this function
return topOfStack+1;
}
}