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.
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 beginning of the array. Use c++ to write this code.
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 beginning of the array. Use c++ to write this code.
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 a c++ program to perform the following operations on stack of Integers (Array Implementation of...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of Stack with maximum size MAX) (i) Push an Element on to stack (ii) Pop an Element from stack (iii) Demonstrate how stack can be used to check Palindrome (iv) Display the status of stack (v) Exit
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...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
create a stack class based on an array of type double of fixed size. In the...
create a stack class based on an array of type double of fixed size. In the class, The array should have the default size of 5. The array size should be settable via a constructor. The following methods must be implemented: push – inserts a value a the top of the stack pop – returns the top value, removing it from the stack isEmpty – returns true if the stack is empty, false otherwise isFull - – returns true if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT