Question

In: Computer Science

Write a ADT called in minStack that provides the following methods: • push() // inserts an...

Write a ADT called in minStack that provides the following methods:

• push() // inserts an element to the “top” of the minStack

• pop() // removes the last element that was pushed on the stack

• top () // returns the last element that was pushed on the stack

• min() // returns the minimum value of the elements stored so far

Solutions

Expert Solution

class MinStack {

    static class Node {
        private int value;
        private Node next;

        public Node(int number) {
            this.value = number;
            next = null;
        }

        public Node(int number, Node next) {
            this.value = number;
            this.next = next;
        }
    }

    private Node head;

    public MinStack() {
        head = null;
    }

    public void push(int num) {
        head = new Node(num, head);
    }

    public int pop() {
        int result = head.value;
        head = head.next;
        return result;
    }

    public int top() {
        return head.value;
    }

    public int min() {
        int minValue = head.value;
        Node temp =head;
        while (temp != null) {
            if (temp.value < minValue)
                minValue = temp.value;
            temp = temp.next;
        }
        return minValue;
    }

    public boolean isEmpty() {
        return head == null;
    }
}

class MinStackTest {

    public static void main(String[] args) {
        MinStack stack = new MinStack();
        stack.push(4);
        stack.push(2);
        stack.push(9);
        System.out.println("Min: " + stack.min());
        stack.push(1);
        System.out.println("Min: " + stack.min());
        System.out.print("Stack: ");
        while (!stack.isEmpty()) {
            System.out.print(stack.pop() + " ");
        }
        System.out.println();
    }
}


Related Solutions

Create a dynamic array-based Queue ADT class in C++ that contains enqueue(Inserts newElement at the back...
Create a dynamic array-based Queue ADT class in C++ that contains enqueue(Inserts newElement at the back ) and dequeue(Removes the frontmost element ). If the size of the array is equal to the capacity a new array of twice the capacity must be made. The interface is shown: class Queue { private: int* elements; unsigned elementCount; // number of elements in the queue unsigned capacity; // number of cells in the array unsigned frontindex; // index the topmost element unsigned...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain strings             red, orange, yellow, blue, indigo, violet write the statement to insert the String element “pink” to the end of the list. Assume the front of the list is on the left. 2. Outline the basic steps to remove a node from the beginning of a list. Completed answers will be given an immediate upvote :)
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
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;...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Add a new method called insert_in_order() which accepts a number as its parameter and inserts that...
Add a new method called insert_in_order() which accepts a number as its parameter and inserts that number in the linked list in an ascending (sorted) order. Note that this method should be called for all the numbers (1, 5, 19, 7, 23, 17, 2) and the resulting linked list should come out sorted. (1, 2, 5, 7, 17, 19, 23) JAVA CODE BELOW class aNode { char data; aNode next; aNode(char mydata) { // Constructor data = mydata; next =...
User ADT: Describes and manipulates user information. You must track the following information about a user / provide the following methods:
• User ADT: Describes and manipulates user information. You must track the following information about a user / provide the following methods:o usernameo firstNameo lastNameo a list of the 10 most recently purchased itemso A user can bid on and purchase ItemsThe User class should have a default constructor, as well as one accepting all parameters. It should also provide accessor (getter) and mutator (setter) methods for appropriate methods. By default, a user is able to buy products only (not...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT