Question

In: Computer Science

Why do we need a dynamic stack and How to implement a dynamic array stack? (...

Why do we need a dynamic stack and How to implement a dynamic array stack? ( Please answer in Java)

Solutions

Expert Solution

A stack stores elements in an ordered list and allows insertions and deletions at one end of the list in O(1) time. The dynamic stack array implements a stack using an array. The size of the array may be changed dynamically after insertions or deletions. For run-time requirements, the number of elements in the stack is n.

Example in Java:

public class DynamicArrayStack {
    public static void main(String[] args) {

        DynamicStack dstack=new DynamicStack(2);
        System.out.println("--Pushing--");
        dstack.push(1);
        dstack.push(2);
        dstack.display();
        dstack.push(3);
        dstack.push(2);
        dstack.push(5);
        dstack.display();
        System.out.println("--Popping--");
        dstack.pop();
        dstack.pop();
        dstack.pop();
        dstack.display();

    }

}

class DynamicStack {
    private int top;
    private int capacity;
    private int[] array;

    public DynamicStack(int cap) {
        capacity = cap;
        array = new int[capacity];
        top = -1;
    }

    public void push(int data) {
        if (isFull()){
            expandArray();      //if array is full then increase its capacity
        }
        array[++top] = data;    //insert the data
    }

    public void expandArray() {
        int curr_size = top + 1;
        int[] new_array = new int[curr_size * 2];
        for(int i=0;i<curr_size;i++){
            new_array[i] = array[i];
        }
        array = new_array;              //refer to the new array 
        capacity = new_array.length;
    }

    public boolean isFull() {
        if (capacity == top+1)
            return true;
        else
            return false;
    }

    public int pop() {
        if (isEmpty()) {
            System.out.println("Stack is empty");
            return -1;
        } else {
            reduceSize();                 //function to check if size can be reduced
            return array[top--];
        }
    }

    public void reduceSize() {
        int curr_length = top+1;
        if (curr_length < capacity / 2) {
            int[] new_array = new int[capacity / 2];
            System.arraycopy(array, 0, new_array, 0, new_array.length);
            array = new_array;
            capacity = new_array.length;
        }
    }

    public boolean isEmpty() {
        if (top == -1)
            return true;
        else
            return false;
    }

    public void display() {
        for (int i = 0; i <= top; i++) {
            System.out.print(array[i] + "=>");
        }
        System.out.println();
        System.out.println("ARRAY SIZE:" + array.length);
    }

}

-----------------------

The code above shows pushing and poping in java language in dynamiv array stack.

-----------------------------------------------------Please Upvote-------------------------------------------------------------


Related Solutions

(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
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...
C programming - Implement a dynamic array, please read and implement the dynarray.c file with what...
C programming - Implement a dynamic array, please read and implement the dynarray.c file with what is given dynarray.h /* * This file contains the definition of the interface for the dynamic array * you'll implement. You can find descriptions of the dynamic array functions, * including their parameters and their return values, in dynarray.c. You * should not modify anything in this file. */ #ifndef __DYNARRAY_H #define __DYNARRAY_H /* * Structure used to represent a dynamic array. */ struct...
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...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
Intuitively, what is the discount rate, why do we need it, and how do we choose...
Intuitively, what is the discount rate, why do we need it, and how do we choose the appropriate number?
sing arrays please write a program to implement the STACK concept after creating the Array, the...
sing arrays please write a program to implement the STACK concept after creating the Array, the user is to be presented with a menu to choose from a number of options such as pop, push, top, etc... elements to be added on the stack are ints between 0 and 99 include a loop to re display the options (menu) and an outer loop to restart the program Write a C++ program to implement the Stacks concept. the stack implementation is...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT