Question

In: Computer Science

Given a class Stack with the interface public void push(char n) // pushes n onto stack...

Given a class Stack with the interface

   public void push(char n) // pushes n onto stack

   public char pop() // return the top of the stack, removing element from stack

   public boolean isEmpty() // return true if stack is empty

Write a method

public int removeX(Stack<Character> stack)

which takes a stack of Characters, removes the occurrences of ‘X’ and returns the count of the number of Xs removed. It must restore the stack to its original order (less the Xs). You may use any other internal storage you choose.

For example, input of stack

      Bottom [ A X B C X D] Top

Would return 2 and the stack would now be

       Bottom [A B C D] Top

Solutions

Expert Solution

import static java.lang.System.exit; 
  
// Create Stack Using Linked list 
class StackUsingLinkedlist { 
  
    // A linked list node 
    private class Node { 
  
        char data; // integer data 
        Node link; // reference variable Node type 
    } 
    // create global top reference variable global 
    Node top; 
    // Constructor 
    StackUsingLinkedlist() 
    { 
        this.top = null; 
    } 
  
    // Utility function to add an element x in the stack 
    public void push(char x) // insert at the beginning 
    { 
        // create new node temp and allocate memory 
        Node temp = new Node(); 
  
        // check if stack (heap) is full. Then inserting an 
        //  element would lead to stack overflow 
        if (temp == null) { 
            System.out.print("\nHeap Overflow"); 
            return; 
        } 
  
        // initialize data into temp data field 
        temp.data = x; 
  
        // put top reference into temp link 
        temp.link = top; 
  
        // update top reference 
        top = temp; 
    } 
  
    // Utility function to check if the stack is empty or not 
    public boolean isEmpty() 
    { 
        return top == null; 
    } 
  
    // Utility function to pop top element from the stack 
    public Node pop() // remove at the beginning 
    { 
        // check for stack underflow 
        if (top == null) { 
            System.out.print("\nStack Underflow"); 
            return null; 
        } 
  
        Node temp = top;
        // update the top pointer to point to the next node 
        top = (top).link; 
        return temp;
    } 
  
    public void display() 
    { 
        // check for stack underflow 
        if (top == null) { 
            System.out.printf("\nStack Underflow"); 
            exit(1); 
        } 
        else { 
            Node temp = top; 
            System.out.print("[ ");
            while (temp != null) { 
  
                // print node data 
                System.out.print(temp.data + " "); 
  
                // assign temp link to temp 
                temp = temp.link; 
            }
            System.out.print("]");
        } 
    } 
    
    // Function to delete all X 
    // elements from the stack 
    public void removeX() 
    { 
        StackUsingLinkedlist temp = new StackUsingLinkedlist(); 
  
        // While stack is not empty 
        while (!isEmpty()) { 
            char val = pop().data; 
  
            // If value is odd then push 
            // it to the temporary stack 
            if (val != 'X') 
                temp.push(val); 
        } 
  
        // Tranfer the contents of the temporary stack 
        // to the original stack in order to get 
        // the original order of the elements 
        while (!temp.isEmpty()) 
            push(temp.pop().data); 
    } 
} 
// main class 
public class Main { 
    public static void main(String[] args) 
    { 
        // create Object of Implementing class 
        StackUsingLinkedlist obj = new StackUsingLinkedlist(); 
        // insert Stack value 
        obj.push('D'); 
        obj.push('X'); 
        obj.push('C'); 
        obj.push('B'); 
        obj.push('X'); 
        obj.push('A'); 
  
        // print Stack elements 
        obj.display(); 
        
        obj.removeX();
        System.out.println("\nAfter Removing X: ");   
        obj.display(); 
    } 
} 

Output:


Related Solutions

void Stack :: push(float x) {   if (count _______ capacity)                               
void Stack :: push(float x) {   if (count _______ capacity)                                                    // If there is no room for a new value { float* new_A = ______ float[ 2 * ________ ];                     // Create a new array with two-fold capacity for (int i = 0; i < count; ++i)                                   // Copy existing data to new array _______= A[i]; delete[]__________ ;                                                          // Delete old array A = new_A;                                                            // Connect to the new array capacity *= _______ ;                                                    // Update the capacity } A[ ______] =...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk();...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk(); public void crash(); public void drive(); } public interface Boat{ public String getMake (); public void setMake (); public void blast_horn(); public void sink(); public void move(); } 1. Create a concrete FamilyCar class from the Car interface.
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField {...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField { public JTextField(){} public void setText(String text) {} public String getText() {} } Write the code to create a JButton on the South of the window and a JTextField on the North. The first time you click on the button, it should print out “hello!” on the JTextField. For the second time, should show “hello!hello!”. For the third time, should show “hello!hello!hello!”.
Given a Queue of Integers with the interface: public void enqueue(Integer i) // add to end...
Given a Queue of Integers with the interface: public void enqueue(Integer i) // add to end public Integer dequeue() // remove from front public boolean isEmpty() // return true if empty Write a method rearrange(Queue q) that takes a queue of integers as a parameter and rearranges the order of the values so that all of the even values appear before the odd values and that otherwise preserves the original order of the list. For example, if a Queue contained...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    *...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    * Gets the current number of entries in this set.    *    * @return The integer number of entries currently in the set.    */    public int getSize();    /**    * Sees whether this set is empty.    *    * @return True if the set is empty, or false if not.    */    public boolean isEmpty();    /**    *...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class...
(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObject and implements Colorable. Design another class named Triangle that extends GeometricObject and implements Colorable. Implement howToColor in Square to display the message Color all four sides. Implement howToColor in Triangle to display the message Color all three sides. Draw a UML diagram that involves Colorable, Square, Triangle,...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
We have created an ArrayList of Person class. write a method called push that pushes all...
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList Content of the ArrayList before push [alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte] content of the ArrayList after the push method [alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex] import java.util.*; class Person { private String name;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT