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

Complete the following JAVA methods with given instructions. import java.util.Em

ID: 3696374 • Letter: C

Question

Complete the following JAVA methods with given instructions.

import java.util.EmptyStackException;
public class ArrayBasedStack<E>
{
private E[] data;
private int topOfStack;
private static final int INITIAL_CAPACITY = 5;

public ArrayBasedStack(int capacity)
{
data = (E[]) new Object[capacity];
topOfStack= -1;
}

public ArrayBasedStack() {
this(INITIAL_CAPACITY);
}
private void expand(int amount)
{
E[] temp = (E[]) new Object[data.length + amount];

for (int i = 0; i <= topOfStack; i++)
{
temp[i] = data[i];
}
data = temp;
}
public void push(E newElement)
{
if (topOfStack == data.length - 1)
{
expand((data.length * 3) / 2 + 1);

}
topOfStack++;
data[topOfStack] = newElement;
}
public boolean isEmpty() {
return topOfStack < 0;
}
public E pop()
{
if(isEmpty())
{
throw new EmptyStackException();
}

E returnObj = data[topOfStack];
data[topOfStack] = null;
topOfStack--;
return returnObj;
}
public E peek()
{
if (isEmpty()) {
throw new EmptyStackException();
}
return data[topOfStack];

}
}

import java.util.EmptyStackException;
public class LinkedStack {
private SinglyLinkedNode topNode = null;
public void push(E newElement) {
// create a node that holds the new element
// set the next node reference of the new node to the current top
// set the top node to reference the new element

}

public boolean isEmpty() {
// empty if the topNode is null

}

public E pop() {
// throw an exception if stack is empty
// store data from current top node (type E)
// set the top node to reference the next node in the stack
// return the stored data

}

public E peek() {
// throw an exception if stack is empty
// return the data from the top node

}
}

Explanation / Answer

import java.util.EmptyStackException;
public class LinkedStack {
private SinglyLinkedNode topNode = null;
public void push(E newElement) {
// create a node that holds the new element
   SinglyLinkedNode n=new SinglyLinkedNode(newElement);

// set the next node reference of the new node to the current top
   n.next=topNode;
// set the top node to reference the new element
   topNode=n;
}
public boolean isEmpty() {
// empty if the topNode is null
   if(topNode==null){
       return true;
   }
   else{
       return false;
   }
}
public E pop() {
// throw an exception if stack is empty
   if(topNode==null){
       System.out.println("Exception thrown :" + "Stack is empty");
   }
// store data from current top node (type E)
   E d=topNode.data();
// set the top node to reference the next node in the stack
   topNode=topNode.next();
// return the stored data
   return d;
}
public E peek() {
// throw an exception if stack is empty
   if(topNode==null){
       System.out.println("Exception thrown :" + "Stack is empty");
   }
// return the data from the top node
   return topNode.data();

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote