1. Using an array For each implementation define the following functions 1. func
ID: 3715511 • Letter: 1
Question
1. Using an array
For each implementation define the following functions
1. function push that pushes a value onto the stack
2. function pop that pops a value off the stack
3. function Top that returns the top value of the stack without popping the stack
4. function isEmpty that determines if the stack is empty
5. function printStack that prints the stack
IMPORTANT!!!!!!!!!!
Write a main (tester) that shows the usage of every function using a Student Object (or any object that has a string and an integer private data members)
Explanation / Answer
/**
An interface for the ADT stack.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public interface StackInterface<T>
{
/** Adds a new entry to the top of this stack.
@param newEntry An object to be added to the stack. */
public void push(T newEntry);
/** Removes and returns this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty before the operation. */
public T pop();
/** Retrieves this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty. */
public T peek();
/** Detects whether this stack is empty.
@return True if the stack is empty. */
public boolean isEmpty();
/** Removes all entries from this stack. */
public void clear();
} // end StackInterface
============================
import java.util.Arrays;
import java.util.EmptyStackException;
/**
* A class of stacks whose entries are stored in an array.
*
* @author Frank M. Carrano
* @author Timothy M. Henry
* @version 4.0
*/
public final class ArrayStack<T> implements StackInterface<T> {
private T[] stack; // Array of stack entries
private int topIndex; // Index of top entry
private boolean initialized = false;
private static final int DEFAULT_CAPACITY = 50;
private static final int MAX_CAPACITY = 10000;
public ArrayStack() {
this(DEFAULT_CAPACITY);
} // end default constructor
public ArrayStack(int initialCapacity) {
checkCapacity(initialCapacity);
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempStack = (T[]) new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
initialized = true;
} // end constructor
public void push(T newEntry) {
checkInitialization();
ensureCapacity();
stack[topIndex + 1] = newEntry;
topIndex++;
} // end push
public T peek() {
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
return stack[topIndex];
} // end peek
public T pop() {
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else {
T top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
return top;
} // end if
} // end pop
public boolean isEmpty() {
return topIndex < 0;
} // end isEmpty
public void clear() {
checkInitialization();
// Remove references to the objects in the stack,
// but do not deallocate the array
while (topIndex > -1) {
stack[topIndex] = null;
topIndex--;
} // end while
// Assertion: topIndex is -1
} // end clear
public void display() {
// YOUR CODE HERE!
if(isEmpty())
System.out.println("The stack is empty.");
else{
System.out.print("BOTTOM ");
for(int i=0;i<=topIndex;++i){
System.out.print(stack[i] + " ");
}
System.out.print("TOP");
}
}
// Throws an exception if this object is not initialized.
private void checkInitialization() {
if (!initialized)
throw new SecurityException(
"ArrayStack object is not initialized properly.");
} // end checkInitialization
// Throws an exception if the client requests a capacity that is too large.
private void checkCapacity(int capacity) {
if (capacity > MAX_CAPACITY)
throw new IllegalStateException("Attempt to create a stack "
+ "whose capacity exceeds " + "allowed maximum.");
} // end checkCapacity
// Doubles the size of the array stack if it is full
// Precondition: checkInitialization has been called.
private void ensureCapacity() {
if (topIndex >= stack.length - 1) // If array is full, double its size
{
int newLength = 2 * stack.length;
checkCapacity(newLength);
stack = Arrays.copyOf(stack, newLength);
} // end if
} // end ensureCapacity
} // end ArrayStack
class Student{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class Driver{
public static void main(String[] args) {
ArrayStack<Student> as = new ArrayStack<Student>();
Student s1 = new Student("Chegg",10);
Student s2 = new Student("RKK",20);
as.push(s1);
as.push(s2);
as.display();
}
}
===================
Thanks, let me know if there is any concern.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.