Question: //---------------------------------------------------------------------- // LinkedStack.java by Dale/Joyce/Weems Chapter 2 // // Implements StackInterface using a linked list to hold the elements. //----------------------------------------------------------------------- package ch02.stacks; import support.LLNode; public

//---------------------------------------------------------------------- // LinkedStack.java by Dale/Joyce/Weems Chapter 2 // // Implements StackInterface using a linked list to hold the elements. //-----------------------------------------------------------------------

package ch02.stacks;

import support.LLNode;

public class LinkedStack implements StackInterface { protected LLNode top; // reference to the top of this stack

public LinkedStack() { top = null; }

public void push(T element) // Places element at the top of this stack. { LLNode newNode = new LLNode(element); newNode.setLink(top); top = newNode; }

public void pop() // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. { if (isEmpty()) throw new StackUnderflowException("Pop attempted on an empty stack."); else top = top.getLink(); }

public T top() // Throws StackUnderflowException if this stack is empty, // otherwise returns top element of this stack. { if (isEmpty()) throw new StackUnderflowException("Top attempted on an empty stack."); else return top.getInfo(); }

public boolean isEmpty() // Returns true if this stack is empty, otherwise returns false. { return (top == null); }

public boolean isFull() // Returns false - a linked stack is never full { return false; }

}

//---------------------------------------------------------------------- // LinkedStack.java by Dale/Joyce/Weems Chapter 2 // // Implements StackInterface using

prints the following. Do not forget to consider the case where the list is empty. Recall that LLNode exports setters and getters for both info and link. Www.ebook 3000.com Exercises a. The sum of the numbers on the list b. The count of how many elements are on the list c. The count of how many positive numbers are on the list d. The enumerated contents of the list; for example, if the list contains 5, 7, and 9 the code will print 1. 5 2. 7 3. 9 e. The reverse enumerated contents of the list (hint: use a stack); for example, if the list contains 5,7 , and 9 the code will print 1. 9 2. 7 3. 5

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!