Question

In: Computer Science

Provide an array-based implementation of stack that allows us adding (pushing) items regardless of the capacity....

Provide an array-based implementation of stack that allows us adding (pushing) items regardless of the capacity. That means, when the stack becomes full, it doubles its capacity to be able to hold more items. Specifically, in your implementation start with default capacity 4, when the stack reaches 4 items and you want to add more, make the capacity 8, and so on. Therefore, you can always add items and the stack expands.

Name the class ImprovedStack and implement the following methods in addition to two constructors, one default no-args constructor, and one constructor that sets the initial capacity.

int pop()

void push(int item)

int peek()

int size()

boolean isEmpty()

boolean isFull()

Assume your items are of type int. your file to be submitted to Gradescope should be named ImprovedStack.java.

Solutions

Expert Solution

//Working Code :-

public class HelloWorld {
static final int capacity = 4; 
static int top = -1; 
static int size = 0; 
 static int[] a;

static int[] size_doubles(int[] a) 
{ 
    int[] new_a = new int[size + capacity];
    for (int i = 0; i < size; i++) 
        new_a[i] = a[i]; 
    size += capacity; 
    return new_a;
} 

static void push(int ele) 
{ 
    if (top == size - 1) 
        a = size_doubles(a); 
    a[++top] = ele; 
} 

static void pop() 
{  
top--; 
} 

static void display(int[] a) 
{
    if (top == -1) 
        System.out.println("Stack is full Empty"); 
    else 
    { 
        System.out.print("Stack: "); 
        for (int i = top ; i >= 0; i--) 
            System.out.print(a[i] + " "); 
        System.out.println();    
 } 
} 

static boolean isEmpty(){
    if (top == -1) 
        return true;
        else
        return false;
}

static boolean isFull(){
    if (top == size-1) 
        return true;
        else
        return false;
}

static int peak (int [] a){
    return a[top];
}

 public static void main(String args[])  
{ 
        a = size_doubles(new int[size + capacity]);

     if(isEmpty())
     System.out.println("Stack is Empty"); 

     push(11); 
     push( 21); 
     push(31); 
     push(41); 
     display(a);

     if(isFull())
     System.out.println("Stack is full "); 
    
     pop();
    
     display(a); 
    
     System.out.println("peak is  "+peak(a)); 
} 
} 
     

//Output :-

//Comments : Intially stack would be empty l , so it should display empty and after inserting 4 elements it should display full , peak element is the top element or the recenly pushed element .


Related Solutions

write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in an array. NOT in linked List. Implement an array-based Linked List in your language. Use double as the item. You need to create a driver includes several items and inserts them in order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE...
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
Please make an Array-based implementation of a Binary Tree in Python based on the given file...
Please make an Array-based implementation of a Binary Tree in Python based on the given file below. Make sure to keep the Abstract Data File of the starter code below when building the Array-based implementation. Python Starter Code Given Below: class ArrayBinaryTree(BinaryTree): """Linked representation of a binary tree structure.""" # -------------------------- nested _Node class -------------------------- class _Node: def __init__(self, element, parent=None, left=None, right=None): # -------------------------- nested Position class -------------------------- class Position(BinaryTree.Position): """An abstraction representing the location of a single element."""...
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
the goal is for you to develop an approach that improves up on the array-based implementation...
the goal is for you to develop an approach that improves up on the array-based implementation in some way.For example, perhaps you want to take an approach that avoids the cost of re-allocating the array when it's full,e.g. using a linked-list. Of course, with a linked-list the cost of accessing an element will jump from O(1) to O(N), so that’s a costly trade-off. Perhaps you can think of a better approach that avoids the cost of reallocating the array, while...
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a...
Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a linked list-based queue ADT that implements the provided QueueInterface.java You may not make any changes to StackInterface.java or QueueInterface.java The classes must be generic The attached generic singly linked list node class, LLNode.java, must be used for the queue implementation; you may not make any modifications to this class Your implementations cannot throw any exceptions Each ADT must include a toString( ) method: The...
TITLE PRIME FACTORIZATION USING AN ARRAY-BASED STACK INTRODUCTION This project revisits one of the problems in...
TITLE PRIME FACTORIZATION USING AN ARRAY-BASED STACK INTRODUCTION This project revisits one of the problems in Project 2, and it will re-use a function within the solution developed there. An integer is prime if it is divisible only by itself and 1. Every integer can be written as a product of prime numbers, unique except for their order, called its prime factorization. For example, 1776 = 37 x 3 x 2 x 2 x 2 x 2. DESCRIPTION Design, implement,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT