Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We need to be able to pop more than one element at a time, discarding the items

ID: 3624538 • Letter: W

Question

We need to be able to pop more than one element at a time, discarding the items popped. To do so, we provide an int parameter count for a popSome method that removes the top count items from the stack.

Q1 Write the member method popSome to be exported from ArrayStack with the following signature: public void popSome (int count)

Q2 Write the member method popSome to be exported from LinkedStack with the following signature: public void popSome (int count)

Q3 Write the method popSome, at the application level, using operations from StackInterface.

Explanation / Answer

Dear,


1.
public void popSome(int count)
{
for(int i=top;i>=0;i--)
{
if(isEmpty())
{ System.out.println("Stack empty");
break;
}
else
{
System.out.println(" Poped:"+stackArray[i]);
top--;
}
}
}

2.

/**
 * List-based implementation of the stack.
 * @author Mark Allen Weiss
 */
public class ListStack implements Stack {
    /**
     * Construct the stack.
     */
    public ListStack( ) {
        topOfStack = null;
    }
    
    /**
     * Test if the stack is logically empty.
     * @return true if empty, false otherwise.
     */
    public boolean isEmpty( ) {
        return topOfStack == null;
    }
    
    /**
     * Make the stack logically empty.
     */
    public void makeEmpty( ) {
        topOfStack = null;
    }
    
    /**
     * Insert a new item into the stack.
     * @param x the item to insert.
     */
    public void push( Object x ) {
        topOfStack = new ListNode( x, topOfStack );
    }
    
    /**
     * Remove the most recently inserted item from the stack.
     * @throws UnderflowException if the stack is empty.
     */
    public void pop( ) {
        if( isEmpty( ) )
           System.out.println( "Empty" );

       else
        topOfStack = topOfStack.next;
    }
   public void somePop(int count)

   {

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

         pop();

}

3.

         Assuming stack interface as

public interface Stack
{

public boolean isEmpty();

public void push(Object obj);

public void pop();
public void somePop(int count);

public Object peek();

}

definition

 public void somePop(int count)

   {

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

         pop();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote