Question: uestion 4: Stack Implementation using an array Consider the following modification of the array-based implementation of a stack. We start with a small array, (of

 uestion 4: Stack Implementation using an array Consider the following modification

uestion 4: Stack Implementation using an array Consider the following modification of the array-based implementation of a stack. We start with a small array, (of size 2, say). When the array becomes full, we create a new array of twice the size as the current one, copy over all the elements of the stack into the new array and keep going. Pseudocode for this implementation is shown below. Stack implementation with dynamically resizing array Initialize(): A= new array of size 2 next =1 Push(a): i= length(A) if next >i: B= new array of length 2i copy(A, B, i) A=B A[ next ]=a next = next +1 Pop(): if next >1: next = next 1 return A[next] Suppose that we perform a sequence of push and pop operations for n times. What is the total running time of all the operations? You may assume the following - copy(A,B,i) is a function that copies the first i elements of A into the corresponding positions in B. It takes (i) time - Finding the length of an array, creating a new array, and assigning a value in an array takes (1) time

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!