Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ArrayStack.java package stacks; public class ArrayStack implements StackADT { private E stackArray[]; // Array holding stack private static final int DEFAULT_SIZE = 10; private int

image text in transcribed

ArrayStack.java

package stacks;

public class ArrayStack implements StackADT {

private E stackArray[]; // Array holding stack private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of stack private int top; // First free position at top

// Constructors @SuppressWarnings("unchecked") // Generic array allocation public ArrayStack(int size) { maxSize = size; top = 0; stackArray = (E[]) new Object[size]; // Create stackArray }

public ArrayStack() { this(DEFAULT_SIZE); }

// Modify this method // so that it all elements are null public void clear() { top = 0; } // Reinitialize stack

// Push "element" onto stack // Changed return type of method to void public void push(E element) { // modify this method // if (top >= maxSize) // move this to a private method called isFull() return false; // call growStack() instead // no changes after this stackArray[top++] = element; // return true; // you may remove this line altogether }

// Remove and return top element public E pop() { if (top == 0) return null; return stackArray[--top]; }

public E peek() { // Return top element if (top == 0) return null; return stackArray[top - 1]; }

public int length() { return top; } // Return stack size

public boolean isEmpty() { return top == 0; } }

LinkedStack.java

package stacks;

import exceptions.EmptyCollectionException; import support.LNode;

public class LinkedStack implements StackADT {

private int length; //keeps track of how many elements exist in the stack private LNode topNode; public LinkedStack( ) { length = 0; topNode = null; } @Override public void push(T element) { // TODO Auto-generated method stub LNode newNode = new LNode(element); newNode.setNext(topNode); topNode = newNode; length++; }

@Override public T pop() { // TODO Auto-generated method stub if (isEmpty()) throw new EmptyCollectionException("linked stack"); T retData = topNode.getData(); topNode = topNode.getNext(); length--; return retData; }

@Override public T peek() { // TODO Auto-generated method stub if (isEmpty()) throw new EmptyCollectionException("linked stack"); return topNode.getData(); }

@Override public boolean isEmpty() { // TODO Auto-generated method stub return (topNode == null); }

@Override public int length() { // TODO Auto-generated method stub return length; }

}

Modify both the ArrayStak and LinkedStack to include the new behaviors: 1. Add a new public method + contains(T element) : boolean - returns true if the stack contains the specified element, false otherwise 2. + search(T element): int - Returns the position where an object is on this stack. If the element exists as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1. The method returns -1 if the element is not found in the stack. 3. + toArray(T[] anArray): void - Returns an array containing all of the elements in this list in proper sequence (from top to bottom element). This method may throw the following exceptions: a. NullPointerException - if the array is null (use the java NullPointerException) b. ArrayCapacityException - if the size of the array is not sufficient to store the elements of the queue (create this exception)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 11 The Database For Big Data And Analytics

Authors: Cristian Molaro, Surekha Parekh, Terry Purcell, Julian Stuhler

1st Edition

1583473858, 978-1583473856

More Books

Students also viewed these Databases questions