Question

In: Computer Science

Write a C++ class that implement two stacks using a single C++ array. That is, it...

Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing).

Notes:

  • Complete all the functions in exercise_2.cpp, then submit this cpp file.
  • pop_first() and pop_second() should throw std::out_of_range exception when stack is empty.

CODE:

#include <cstdio>

#include <stdexcept>

template <class T>

class TwoStacks {

public:

  // Constructor, initialize the array to size_hint

  TwoStacks(size_t size_hint = 16) : size_(size_hint), array_(new T(size_hint)) {}

  // Destructor

  ~TwoStacks() { delete array_; }

  // Push a value to the first stack

  void push_first(const T& val) {

    // Implement here

    

  }

  // Push a value to the second stack

  void push_second(const T& val) {

    // Implement here

  }

  // Pop from the first stack and return the value

  T pop_first() {

    // Implement here, throw std::out_of_range if necessary

  }

  // Pop from the second stack and return the value

  T pop_second() {

    // Implement here, throw std::out_of_range if necessary

  }

  // Return the size of the first stack

  size_t size_first() const {

    // Implement here

  }

  // Return the size of the second stack

  size_t size_second() const {

    // Implement here

  }

  // Return true if the first stack is empty

  bool empty_first() const { return size_first() == 0; }

  // Return true if the second stack is empty

  bool empty_second() const { return size_second() == 0; }

private:

  size_t size_;

  T *array_;

};

Solutions

Expert Solution

CODE:

#include <cstdio>

#include <stdexcept>

template <class T>

class TwoStacks {

public:

   // Constructor, initialize the array to size_hint

   TwoStacks(size_t size_hint = 16) : size_(size_hint), array_(new T(size_hint)) {}

   // Destructor

   ~TwoStacks() { delete array_; }

   // Push a value to the first stack

   void push_first(const T& val) {

       if(current_size_ >= size_)
       {
           array_ = (T*)realloc(array_, size_ * 2);
           size_ *= 2;
       }
      
       array_[current_size_++] = val;

   }

   // Push a value to the second stack

   void push_second(const T& val) {

       if (current_size_ >= size_)
       {
           array_ = (T*)realloc(array_, size_ * 2);
           size_ *= 2;
       }

       array_[current_size_++] = val;

   }

   // Pop from the first stack and return the value

   T pop_first() {

       // Implement here, throw std::out_of_range if necessary
       if (current_size_ == 0)
           throw std::out_of_range("Out of range exception");
      
       T temp = array_[current_size_ - 1];

       --current_size_;
      
       return temp;
   }

   // Pop from the second stack and return the value

   T pop_second() {

       if (current_size_ == 0)
           throw std::out_of_range("Out of range exception");

       T temp = array_[current_size_ - 1];

       --current_size_;

       return temp;

   }

   // Return the size of the first stack

   size_t size_first() const {

       return current_size_;

   }

   // Return the size of the second stack

   size_t size_second() const {

       return current_size_;

   }

   // Return true if the first stack is empty

   bool empty_first() const { return size_first() == 0; }

   // Return true if the second stack is empty

   bool empty_second() const { return size_second() == 0; }

private:

   size_t size_;

   T* array_;

   size_t current_size_ = 0;

};


Related Solutions

1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Show how to implement three stacks in one array (in Java)
Show how to implement three stacks in one array (in Java)
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable array size. Your class should support storing an integer at a specific index value, retrieving the integer at a specific index value, and automatically increasing storage for the saved values.
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT