Question

In: Computer Science

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)

Solutions

Expert Solution

we know that stack uses LIFO princliple it stores value. we give size of the stack but if we have more data to store we want to increase the size for everything

for exampe: stack=new int[3];

in the above example the stack size is 3 but we have more than three datas then we want to double the stack then we have six size to fil datas but we want more than 6 then we again double the stack size thats why we are using dynamic stack.dynamic stack is like dynamic array we can change the length by using operations.

if we want to use more data than the stack size we can use the dynamic stack with the help of dynamic stack we can double the data size

we can implement dynamic array stack using creating a new function and within the function we can double the size by using length function.

for example

private void dousize(){

int temp[]=x;

x=new int[2*temp.length];

for(int i=0;i<=top;i++)

x[i]=temp[i];}

in the above example we create a function called dousize() we call the function in main.

we are creating an array name temp and assigning to x next line we double the size using length and using for loop.


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?
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...
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...
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...
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...
Show how to implement three stacks in one array (in Java)
Show how to implement three stacks in one array (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...
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.
How do we define the stack ADT, including its operations? Typed please.
How do we define the stack ADT, including its operations? Typed please.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT