Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following routine removes the first half of the list passed as a parameter: public static void removeFirstHalf( List lst ) { int theSize =

The following routine removes the first half of the list passed as a parameter:
public static void removeFirstHalf( List lst )
{
int theSize = lst.size( ) / 2;
for( int i = 0; i < theSize; i++ )
lst.remove( 0 );
}
a. Why is theSize saved prior to entering the for loop?
b. What is the running time of removeFirstHalf if lst is an ArrayList?
c. What is the running time of removeFirstHalf if lst is a LinkedList?
d. Does using an iterator make removeHalf faster for either type of List?

For part a, what would happen if the for loop was written as?

for (int i = 0 ; i < lst.size()/2 ; i++) {
   lst.remove(0) ;
}

How much of the list would be removed? Why?

Step by Step Solution

3.41 Rating (164 Votes )

There are 3 Steps involved in it

Step: 1

a In the given routine theSize is saved prior to entering the for loop because according to the valu... 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

Data Structures and Algorithm Analysis in Java

Authors: Mark A. Weiss

3rd edition

132576279, 978-0132576277

More Books

Students also viewed these Algorithms questions

Question

Question I

Answered: 1 week ago