Question: Need some help with java code. #3 CODE Array stack. ublic class ArrayStack { int[] stack; // Array containing the data in the stack int
Need some help with java code.

#3 CODE Array stack.
ublic class ArrayStack {
int[] stack; // Array containing the data in the stack
int top; // Index representing the top of the stack
// Constructor
// Preallocates an array of size 10 to be used as a stack
// Initializes the top of the stack
ArrayStack() {
stack = new int[10];
top = 0;
}
// Increases the capacity of the stack by making
// a new array that is double the size of the previous one
void makeNewArray() {
int[] newStack = new int[stack.length*2];
for (int i=0; i newStack[i] = stack[i]; stack = newStack; } // Implements the push operation void push(int e) { if (top==stack.length) { makeNewArray(); } stack[top] = e; top++; } // Implements the pop operation int pop() { top--; return stack[top]; } // Implements the peek operation int peek() { return stack[top-1]; } // Implements the isEmpty operation boolean isEmpty() { return top==0; } // Implements the size operation int size() { return top; } } #3--- #4 CODE LINKEDSTACK public class LinkedStack { LinkedNode front; // Reference to the first LinkedNode in the list int count; // Number of nodes in the list // Constructor - initializes the front and count variables LinkedStack() { front = null; count = 0; } // Implements the push operation void push(int x) { LinkedNode newNode = new LinkedNode(x); newNode.next = front; front = newNode; count++; } // Implements the pop operation int pop() { int x = front.x; front = front.next; count--; return x; } // Implements the peek operation int peek() { return front.x; } // Implements the isEmpty operation boolean isEmpty() { return front==null; } // Implements the size operation int size() { return count; } // This method returns a String containing // a space separated representation of the underlying linked list public String toString() { String str = ""; LinkedNode cur = front; while (cur!=null) { str += cur.x + " "; cur = cur.next; } return str; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
