Question

In: Computer Science

3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...

3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation.

import java . io .*;

import java . util .*;

public class Stack2540Array {

int CAPACITY = 128;

int top ;

String [] stack ;

public Stack2540Array () {

stack = new String [ CAPACITY ];

top = -1;

}

public int size () { return top + 1; }

public boolean isEmpty () { return ( top == -1) ; }

public String top () {

if ( top == -1)

return null ;

return stack [ top ];

}

public void push ( String element ) {

top ++;

stack [ top ] = element ;

}

Solutions

Expert Solution


Logic for popping an element from a stack of array type:-

Removes an item from the stack. The items are popped in the reversed order in which they are pushed initially. If the stack is empty, then it is said to be an Underflow condition. We would implement this with an if-else statement, where the condition will be checking if the stack is empty i.e., top == -1. Since you have created a function to check the same, isEmpty(), we will use this statement in the condition for if-block

Code for pop() in stack ADT using array:-

String pop()
    {
        if (isEmpty()) //check if Stack is empty
        {
            return "Stack is empty";
        }
        else //If not empty, then pop the next element
        {
            String ele = top(); //Assign top to new string element

            top--; //Removing top element from the Stack

            return ele; //Return the popped element
        }
    }

In case, you have any doubt.. Please ask in the comment section.

Additional Information:-

I am also providing you a sample driver code and the output for the same.

Driver code:-

public static void main(String args[])
    {
        //Declare a new class object
        Stack2540Array s = new Stack2540Array();
        //Push some elements into the stack
        s.push("Hello");
        s.push("How");
        s.push("Are");
        s.push("You");
        //pop the top element
        System.out.println(s.pop() + " : Popped from stack");
        System.out.println(s.pop() + " : Popped from stack");
        System.out.println(s.pop() + " : Popped from stack");
        System.out.println(s.pop() + " : Popped from stack");
    }

Output:-

Thank you.

Happy Learning.


Related Solutions

3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array...
3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. The implementation can be found in the book and in our lecture slides. import java . io .*; import java . util .*; public class Stack2540Array...
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS:...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS: Not allowed to use ANY built-in Python data structures and their methods. You must solve by importing the DynamicArray class and using class methods to write solution. Also not allowed to directly access any variables of the DynamicArray class (like self.size, self.capacity and self.data in part 1). All work must be done by only using class methods. Below is the Bag ADT starter code...
Consider an ADT with the same operators as a stack (push(), pop(), size(), empty()), except that...
Consider an ADT with the same operators as a stack (push(), pop(), size(), empty()), except that the pop() operator returns the largest element rather than the element most recently pushed. For this to make sense, the objects on the stack must be totally ordered (e.g. numbers). Describe array and linked list implementations of the ADT. Your implementations should be as time and space efficient as you can manage. Give the running times for the CRUD operators.
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure. Related codes: public interface Stack<E> { int size( ); boolean isEmpty( ); void push(E e); E top( ); E pop( ); } public class LinkedStack<E> implements Stack<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>( );    public LinkedStack( ) { }    public int size( ) { return...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
# ArrayStack # TODO: push and pop using array stack with doubling technique. import numpy as...
# ArrayStack # TODO: push and pop using array stack with doubling technique. import numpy as np from future import print_function # Definisi class ArrayStack class ArrayStack(object): def __init__(self): self.data = np.zeros(20, dtype=np.int) self.count = 0 def isEmpty(self): pass # ubah saya def size(self): pass # ubah saya def push(self, obj): pass # ubah saya def pop(self): pass # ubah saya #-------------------------- # End of class if __name__ == "__main__": stack = ArrayStack() randn = np.random.permutation(1000) for i in range(1000):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT