Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1: Add the method below that prints every other element in the stack, starting with the top element. You can assume there are an

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Problem 1: Add the method below that prints every other element in the stack, starting with the top element. You can assume there are an even number of elements in the stack. For example, if the stack contains the elements 2 4 6 8 10 12, where 2 is the top, then this method will print 26 10". public void printEveryother () { Problem 2: Add the method below that adds the given element to the stack...except it is added as the second element, not the top element. For example, if the stack contains 2 4 6 8 10 12, and we want to add 7, then the stack will be 2 7 4 6 8 10 12. Throw the EmptyStackException if the stack is empty. public void addSecond (E element) { Problem 3: Add the method below that removes and returns the bottom element from the stack. For example, if the stack contains 2 4 6 8 10 12, then this method will remove the 12. Do not add a new field variable to the class. public E removeBottom () { Problem 4: Add the method below that returns an array containing the elements that are in the stack, in the same order (where the top element will be the first element in the array). The array will be an Object array and should have the same size as the stack. The method must not make any changes to the stack. For example, if the stack contains 2 4 6 8 10 12, then this method will return the array 24681012 public Object[] toArray() { public class LinkedStack implements Stack { // Node objects are linked together in the stack private class Node { E element; // Element stored in this node Node link; // Link to next node in stack } private Node top; // Reference to node containing top element private int size; // Number of elements in the stack * Constructor - creates an empty stack. public LinkedStack() { top = null; size = 0; } @Override stack. * Adds the given element to the top of the * @param elem element to add to stack */ public void push(E elem) { Node n = new Node(); n.element = elem; n.link = top; top = n; size++; } public E pop() { if (size == 0) throw new Empty StackException(); E elem = top.element; top = top.link; size--; return elem; } @Override /** * Returns, but does not remove, this stack's top element. * @return top element * @throws EmptyStackException public E peek() { if (size == 0) throw new EmptyStackException(); return top.element; } @Override /** * Returns the number of elements in this stack. * @return size of Stack */ public int size() { return size; } @Override /** * Returns true if this stack is empty. * @return true if stack is empty public boolean empty() { return top == null; } @Override * Returns this stack as a string. * @return string containing this stack's public String toString() { String s = ""; Node walker = top; while(walker != null) { s += walker.element+" walker = walker.link; } return s; } @Override * Returns true if this stack is equal to the given stack. * Two stacks are equal if they have equal elements in the * same order. * @return true if stacks are equal public boolean equals(Object obj) { if (obj instanceof LinkedStack) { LinkedStack s = (LinkedStack) obj; if (size 1= s.size) { return false; } else { Node tWalker top; Node sWalker = s.top; while(tWalker != null) { if(!tWalker.element.equals(swalker.element)) { return false; } tWalker - tWalker.link; sWalker sWalker.link; } return true; } } else { return false; } }

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

Step: 3

blur-text-image

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

Public Finance

Authors: Harvey S. Rosen

5th Edition

025617329X, 978-0256173291

Students also viewed these Databases questions

Question

What is the slope b?

Answered: 1 week ago

Question

Explain the Neolithic age compared to the paleolithic age ?

Answered: 1 week ago

Question

What is loss of bone density and strength as ?

Answered: 1 week ago