Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Stack Manipulation Sometimes its useful to define operations on an ADT without changing the type definition itself. For example, you might want to print the

Stack Manipulation

Sometimes its useful to define operations on an ADT without changing the type definition itself. For example, you might want to print the elements in a stack without actually adding a method to the Stack ADT (you may not even have access to it). To explore this, use either the Stack class provided by Java (in java.util) or one ofthe stack classes that you wrote in anearlier lab exercise and the test program StackTest.java.Add the following static methods to the StackTest class (the signature for these methods and the declaration in StackTest assumes you are using a stack class named Stackmodify them to use the name of your class):

void printStack(Stack s)prints the elements in stack s from top to bottom. When printStack returns, s should be unchanged.

Stack reverseStack(Stack s)returns a new stack whose elements are backwards from those in s. Again, s is unchanged.

Stack removeElement(Stack s, int val)returns a new stack whose elements are the same as those in s (and in the same order) except that all occurrences of val have been removed. Again, s is unchanged.

Modify the main method to test these methods. Be sure you print enough information to see if theyre working!

// ***************************************************************

// StackTest.java

//

// A simple driver to test a stack.

//

// ***************************************************************

import java.util.Stack;

public class StackTest

{

public static void main(String[] args)

{

// Declare and instantiate a stack

Stack stack = new Stack();

//push some stuff on the stack

for (int i=0; i<10; i++)

stack.push(i);

stack.push(5);

// call printStack to print the stack

// call reverseStack to reverse the stack

// call printStack to print the stack again

// call removeElement to remove all occurrences of the value 5 - save the

// stack returned from this call

// call printStack to print the original stack and the new stack.

}

}

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

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago