Question

In: Computer Science

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 is needed. You will modify the push operation by resizing the array when it overflows. And you also need to implement the resize(...) method. The details of the code are in the slides. Submit your dynamic stack in the submission site here. Your can use the doubling strategy in resizing.

import java.io.*;
import java.util.*;

public class Stack2540ArrayDynamic {
   int CAPACITY = 128;
   int top;
   String[] stack;
  
   public Stack2540ArrayDynamic() {
       stack = new String[CAPACITY];
       top = -1;
   }

   public int size() {       return top + 1;   }
   public boolean isEmpty() {       return (top == -1); }

   public String top() {
       if (top == -1)
           return null;
       return stack[top];
   }
      
   // add resize method

   // add pop method

   public void push(String element) {
       // add something here

      top++;
       stack[top] = element;
   }
}

* Please complete the resize, pop, and push methods.

Solutions

Expert Solution

CODE:

import java.io.*;
import java.util.*;

public class Stack2540ArrayDynamic {
   int CAPACITY = 10;//making capacity small first
   int top=-1;
   String[] stack;
  
   public Stack2540ArrayDynamic() {
       stack = new String[CAPACITY];
       top = -1;
   }

   public int size() {       return top + 1;   }
   public boolean isEmpty() {       return (top == -1); }

   public String top() {
       if (top == -1)
           return null;
       return stack[top];
   }
      
   // add resize method
    public void resize() {
    CAPACITY*=2;//using doubling method
    }
   // add pop method
    public String pop() 
        {
            if(isEmpty()) //checking if empty stack
            {
             System.out.println("UNDERFLOW");   
            }
            String t=stack[top--];//removing last element
            return t;
        }
   public void push(String element) {
       // add something here
       if(top>=(CAPACITY-1))
       {
           resize();//If OVERFLOW call resize
       }

      top++;
       stack[top] = element;
   }
}

SCREENSHOT OF CODE:

I hope I solved your query. In case of doubt feel free to ask in the comment section.


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)
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)
(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?
Need to program a maze traversal program using dynamic array and stacks. The problem is faced...
Need to program a maze traversal program using dynamic array and stacks. The problem is faced in setting up the data structure and due the problem in setting the data structure other functions like moving in all direction, marking the current cell that is visited are showing the error. The work so far is below: const int R = 5; //Number of rows const int C = 5; //Number of columns class coordinate { public: int v; int h; };...
use cout to print the address at which the following array is stored in memory long...
use cout to print the address at which the following array is stored in memory long double computers[24] a. print the adress of the last element b. print the address of the tenth element. c. print the address of the first element in the array
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
Please complete the following functions in "queue.c" using C. This must use the dynamic array provided...
Please complete the following functions in "queue.c" using C. This must use the dynamic array provided in "dynarray.c" -------------------------------------------------------------------------------------------- //queue.c #include <stdlib.h> #include "queue.h" #include "dynarray.h" /* * This is the structure that will be used to represent a queue. This * structure specifically contains a single field representing a dynamic array * that should be used as the underlying data storage for the queue. * * You should not modify this structure. */ struct queue { struct dynarray* array;...
Plese answer the question! 1) What kind of memory technologies do we use as memory hierarchy...
Plese answer the question! 1) What kind of memory technologies do we use as memory hierarchy organization in computers?
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT