Question

In: Computer Science

void Stack :: push(float x) {   if (count _______ capacity)                               

void Stack :: push(float x)
{
  if (count _______ capacity)                                                    // If there is no room for a new value
{
float* new_A = ______ float[ 2 * ________ ];                     // Create a new array with two-fold capacity
for (int i = 0; i < count; ++i)                                   // Copy existing data to new array
_______= A[i];
delete[]__________ ;                                                          // Delete old array
A = new_A;                                                            // Connect to the new array
capacity *= _______ ;                                                    // Update the capacity
}
A[ ______] = x;                                                                   // Insert the new item onto the stack
++ ________ ;                                                                          // Update count of items in the stack
}

Solutions

Expert Solution

In case of any query do comment. Thanks

Answer:

void Stack :: push(float x)
{
    if (count == capacity)                                          // If there is no room for a new value
    {
        float* new_A = new float[ 2 * capacity ];                   // Create a new array with two-fold capacity
        for (int i = 0; i < count; ++i)                             // Copy existing data to new array
            new_A[i]= A[i];
        delete[] A ;                                                // Delete old array
        A = new_A;                                                  // Connect to the new array
        capacity *= 2 ;                                             // Update the capacity
    }
    A[count] = x;                                                   // Insert the new item onto the stack
    ++ count;                                                       // Update count of items in the stack
}

Explanation:

Check count == capacity for stack is full or there is no room for new element to push

Declare a new array of the size double of the capacity. (float* new_A = new float[ 2 * capacity ];)

Copy the existing array to new array index wise till count -1 index using for each loop:

for (int i = 0; i < count; ++i) // Copy existing data to new array
new_A[i]= A[i];

delete[] is used top delete memory created by existing array which was initialized using new operator.

hence  delete[] A ;

Then double the capacity:

capacity *= 2 ;

to insert the new item , we have to use count variable which is tracking the current index:

A[count] = x;

now increase the value of count variable by 1:

++ count;


Related Solutions

Given a class Stack with the interface public void push(char n) // pushes n onto stack...
Given a class Stack with the interface public void push(char n) // pushes n onto stack public char pop() // return the top of the stack, removing element from stack public boolean isEmpty() // return true if stack is empty Write a method public int removeX(Stack<Character> stack) which takes a stack of Characters, removes the occurrences of ‘X’ and returns the count of the number of Xs removed. It must restore the stack to its original order (less the Xs)....
This is my current code for a question that asks: "Implement the stack methods push(x) and...
This is my current code for a question that asks: "Implement the stack methods push(x) and pop() using two queues". It works well, other than I want it to work for data of type T, not just Int's. (EXCUSE THE INDENTING... CHEGG POSTING DOESNT ALLOW PROPER CODE TO BE UPLOADED... JUST COPY PASTE IT INTO ECLIPSE IDE, HIGHLIGHT ALL CODE, PRESS COMMAND+SHIFT+F AND SHOULD AUTO-FORMAT) MY QUESTION: How could one modify this code in order to take data of type...
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and...
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and the non-standard min() operation which returns the minimum value stored on the stack. The zip file gives an implementation SlowMinStack that implements these operations so that push(x) and pop() each run in O(1) time, but  min()runs in Θ(n) time. For this question, you should complete the implementation of FastMinStack that implements all three operations in O(1) time per operation. As part of your implementation, you...
Ice cubes usually float in a glass of water. If you push the ice cubes down...
Ice cubes usually float in a glass of water. If you push the ice cubes down to the bottom of the glass (using a straw), would they cool the water more, or less, quickly? Explain.
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use the iterator and comparator, does not need to be a linked list, although it's what I've tried using. I'm using another tester class to test this class. import java.util.Comparator; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.ListIterator; public class FMinStack<T> implements MinStack<T> { protected Comparator<? super T> comp; T min; protected List<T> ds; public FMinStack() { this(new DefaultComparator<T>()); } public FMinStack(Comparator<? super...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT