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...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
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...
JAVA Stack - Implementation. You will be able to use the push, pop and peek of...
JAVA Stack - Implementation. You will be able to use the push, pop and peek of Stack concept. Post-Fix calculator - When an arithmetic expression is presented in the postfix form, you can use a stack to evaluate the expression to get the final value. For example: the expression 3 + 5 * 9 (which is in the usual infix form) can be written as 3 5 9 * + in the postfix. More interestingly, post form removes all parentheses...
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'.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT