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)
Consider an ADT with the same operators as a stack (push(), pop(), size(), empty()), except that...
Consider an ADT with the same operators as a stack (push(), pop(), size(), empty()), except that the pop() operator returns the largest element rather than the element most recently pushed. For this to make sense, the objects on the stack must be totally ordered (e.g. numbers). Describe array and linked list implementations of the ADT. Your implementations should be as time and space efficient as you can manage. Give the running times for the CRUD operators.
JAVA I need to make it that I can expand the stack for everytime a push...
JAVA I need to make it that I can expand the stack for everytime a push operation is performed that would normally cause an overflow, returning a false value. Should accompany 3 nodes initially. DriverStack.java public class DriverStack {    public static void main (String[] args) {    Stack s = new Stack(3); Listing l; Listing l1 = new Listing ("Bill", "1st Avenue", "123 4567"); Listing l2 = new Listing ("Alec", "2nd Avenue", "456 4567"); Listing l3 = new Listing...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT