Question

In: Computer Science

how could I implement an intStack class that has only a push and pop method? in...

how could I implement an intStack class that has only a push and pop method?

in java of course.

Solutions

Expert Solution

// Java code for stack implementation

  

import java.io.*;

import java.util.*;

  

class Test

{   

    // Pushing element on the top of the stack

    static void stack_push(Stack<Integer> stack)

    {

        for(int i = 0; i < 5; i++)

        {

            stack.push(i);

        }

    }

      

    // Popping element from the top of the stack

    static void stack_pop(Stack<Integer> stack)

    {

        System.out.println("Pop :");

  

        for(int i = 0; i < 5; i++)

        {

            Integer y = (Integer) stack.pop();

            System.out.println(y);

        }

    }

  

  

    public static void main (String[] args)

    {

        Stack<Integer> stack = new Stack<Integer>();

  

        stack_push(stack);

        stack_pop(stack);

        stack_push(stack);

    }

}


Related Solutions

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'.
How could I implement the contain and min method for a tree in this code. import...
How could I implement the contain and min method for a tree in this code. import java.util.List; import edu.princeton.cs.algs4.*; class ProgrammingAssignment1 { //////////////////// EXERCICE 1: TWO THREE TREES static public <Key extends Comparable<Key>> int size (NakedTree<Key> tree) { if (tree == null) return 0; int val = tree.getNKeys (); for (int i = 0; i <= tree.getNKeys (); ++i) val += size (tree.getChild (i)); return val; } //////// EXERCICE 1.1: contains AND min static public <Key extends Comparable<Key>> boolean contains...
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
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
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...
You are to write a class StringPlay that has only a main method. This class contains...
You are to write a class StringPlay that has only a main method. This class contains all interaction with the user. The main method Uses a Scanner to accept user input Maintains a “current” String Uses a sentinel-controlled loop to accept multiple user commands, or exit The program displays a message asking the user to enter one of the following commands a – enter a new value for current b – padLeft asks the user for the number of characters...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method...
why and how could we clearly establish POP and POD?
why and how could we clearly establish POP and POD?
how do I write a class PostFix that has one method covert that converts an infix...
how do I write a class PostFix that has one method covert that converts an infix expression (as a string input) to postfix in java?
Please only edit the list.cpp file only, implement the push_front method that will insert a new...
Please only edit the list.cpp file only, implement the push_front method that will insert a new element to the front of the list. //list.h // Doubly linked list #ifndef Q2_H #define Q2_H template<typename T> class List; template<typename T> class Iterator; template <typename T> class Node {    public:        Node(T element);    private:        T data;        Node* previous;        Node* next;    friend class List<T>;    friend class Iterator<T>; }; template <typename T> class List...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT