Question

In: Computer Science

Program in Java Create a stack class to store integers and implement following methods: 1- void...

Program in Java

Create a stack class to store integers and implement following methods:

1- void push(int num): This method will push an integer to the top of the stack.

2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1.

3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value).

4- Boolean isEmpty(): This method will check the stack and if it is empty, this will return true, otherwise false.

Solutions

Expert Solution

class Stack {
        final int MAX = 100;
        private int top;
        private int a[] = new int[MAX]; // Maximum size of Stack
        private int min;

        // constructer calls when the object is created
        Stack() {
                top = -1; // initialize top value -1
        }

        // this method is used to insert values into stack

        public void push(int x) {
                if (top >= (MAX - 1)) // here we check the condition if values are there are not
                {
                        System.out.println("Stack Overflow");
                } else {
                        a[++top] = x;
                }
        }

        // this method is used to delete top element
        public int pop() {
                if (top < 0) // here also we check the condition if values are there are not
                {
                        return -1;
                } else {
                        int x = a[top--];
                        return x;
                }
        }

        public boolean isEmpty() {
                return top < 0;
        }
        public void display() {
                for(int i=top-1;i>=0;i--)
                        System.out.print(a[i]+" ");
                System.out.println();
        }

}

public class TestIntegerStack {
        public static void main(String[] args) {
                Stack s = new Stack();
                System.out.println(s.isEmpty());
                s.push(10);
                s.push(20);
                s.push(30);
                s.push(40);
                s.push(50);
                s.push(60);
                s.display();
                s.pop();
                s.display();
                System.out.println(s.isEmpty());
        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

Please Like and Support me as it helps me a lot


Related Solutions

Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
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...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT