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

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.
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...
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...
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...
Write a C function to implement operation of a stack using the following format: /** *...
Write a C function to implement operation of a stack using the following format: /** * function: *       push * * expects: *       pointer to the stack *       pointer to the size *       the value to push * * returns: *     true when value has been pushed *       false otherwise * * The push function push a value to the passed in stack */ bool push(int *stack, int *size, int max_size, int to_push) {...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT