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 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...
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'.
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
Implement the stack ADT in a fully generic manner (through the use of templates) by means...
Implement the stack ADT in a fully generic manner (through the use of templates) by means of a singly linked list. (Give your implementation “from scratch,” without the use of any classes from the Standard Template Library ).
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. In Java please.
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a...
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a linked list-based queue ADT that implements the provided QueueInterface.java You may not make any changes to StackInterface.java or QueueInterface.java The classes must be generic The attached generic singly linked list node class, LLNode.java, must be used for the queue implementation; you may not make any modifications to this class Your implementations cannot throw any exceptions Each ADT must include a toString( ) method: The...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called Stack. Use a static array to hold stack elements. Instantiate the Stack class in the main function and provide a user loop and a menu so that all the Stack class member-functions, push, pop, etc., are available so that the user can thoroughly exercise the member-functions of the Stack class. Also, implement a ReversePrint() for the stack. My StackProject, whose exposition I have given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT