1. Suppose you are asked to create a new method for the stack class, PopN, defin
ID: 3542234 • Letter: 1
Question
1. Suppose you are asked to create a new method for the stack class, PopN, defined as follows:
PopN(S, n)
{ Removes from stack S the n number of items that were
most recently added. It is an error if there are
fewer than n items on S. }
To implement this operation, you could either modify the stack directly or use the existing Pop operation. What would be the advantages and disadvantages of each method?
Suppose you are asked to create a new method for the stack class, PopN, defined as follows: { Removes from stack S the n number of items that were most recently added. It is an error if there are fewer than n items on S. } To implement this operation, you could either modify the stack directly or use the existing Pop operation. What would be the advantages and disadvantages of each method?Explanation / Answer
Advantages of first method of directly modifying the stack : We can directly check the underflow condition by checking the size of the stack and directly move the pointer to n elements lower(in case of array implementation of stack)
Disadvantages : Program loses modularity
Advantages of second option of using pop function : Modularity is maintained and is simple to implement using the existing pop function.
Disadvantages : we have to pop the elements one by one.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.