Question

In: Computer Science

(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?

Solutions

Expert Solution

The stack is a linear data structure that is used to stores the collection of objects and it follows LIFO. But if you want to store more data you can use the dynamic stack, it is a stack data structure whose length and capacity (max no. of elements can be stored) increases /decreases in real-time based on operations like insertion, deletion,..etc performed on it.

A stack can be implemented by an array but since we are using an array to implement we cannot insert elements more than the size of the array, if we do it'll throw an error "memory overflow" because the size of the array is fixed and that's a problem there. To overcome this problem we use stack dynamic.

To make a stack dynamic we implement stack using LinkedList such stack is called dynamic stack.

How to implement dynamic stack array?

Implementation of dynamic stack:

Save this as Stack.java

Now save this as DynamicStackTest.java

To compile the program:

javac -d .Stack.java

javac -d .DynamicStackTest.java

and to run:

java ClassIllustration.DynamicStackTest

OUTPUT:


Related Solutions

Please Why do we need a dynamic stack? How to implement a dynamic array stack?(JAVA)
Please Why do we need a dynamic stack? How to implement a dynamic array stack?(JAVA)
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)
Dynamic Array (4 marks) To save computer memory, we need to use dynamic array. In the...
Dynamic Array To save computer memory, we need to use dynamic array. In the first version of our stack implementation, the instance variable has a fixed capacity, which is 128. There will be an error when the stack is bigger than 128. For bigger data, you increased the array size. But that very long array could be wasted for smaller data. In order to solve this dilemma, we start with a small array, and increase the capacity only when it...
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...
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...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Show how to implement three stacks in one array (in Java)
Show how to implement three stacks in one array (in Java)
How do you implement an AVL tree for strings in Java?
How do you implement an AVL tree for strings in Java?
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