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)
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...
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 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...
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...
Why do we need carbohydrates and how much do you know about carbohydrates and how they...
Why do we need carbohydrates and how much do you know about carbohydrates and how they contribute to energy in your body.
In Java Script how do you code an array with the values and suits of cards...
In Java Script how do you code an array with the values and suits of cards (all 52 cards in a deck) 2 different array one that just has the face values of the card, and the other with the suit value. Then have it randomly give you 1-2 cards from the deck without repeating? (Example: Dealer gives player two cards. Player asks for a hit and the dealer gives a different card, no repeats). Game is Blackjack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT