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

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)
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.
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT