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)
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...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
Suppose an initially empty stack S has performed a total of 15 push operations, 12 top...
Suppose an initially empty stack S has performed a total of 15 push operations, 12 top operations, and 13 pop operations ( 3 of which returned null to indicate an empty stack ). What is the current size of S? Question 1 options: Question 2 (1 point) Saved What values are returned during the following series of stack operations, if executed upon an initially empty stack? push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7), push(6), pop(), pop(),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT