Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a method on the Queue class that calculates the number of times a give

ID: 3815310 • Letter: 1

Question

1. Create a method on the Queue class that calculates the number of times a given value occurs in the queue.

size_t Queue::count( const T & data ) const;

2. Create a method on the Queue class that determines whether the queue is in descending order.

bool Queue::isDecreasing( ) const;

3. Create a method on the Stack class that determines whether a given value occurs consecutively in the stack.

bool Stack::isConsecutive( const T & data ) const;

4. Create a method on the Stack class that reverses the values on the stack.

void Stack::reverse();

The starter files code is provided below. Do not alter the class definition or driver code in any way. Programs that crash are subject to a 50% penalty. Please submit the class header files only (Queue.h” and “Stack.h”). PLEASE NOTE: You may not use any Standard Template Library (STL) classes for this; use code provided below only.

Please compile the code from your answer and check for errors before submitting code that is not working.

queue.h:

#pragma once

#include

namespace cs20a

{

   template

   class queue {

   public:

       queue();

       explicit queue(size_t capacity);

       queue(const queue &rhs);

       ~queue();

       queue& operator =(const queue & rhs);

       bool operator==(queue & rhs) const;

       bool operator!=(queue & rhs) const;

       void enqueue(const T& val);

       T dequeue();

       T peek() const;

       void clear();

       bool empty() const;

       bool full() const;

       size_t size() const;

       void print();

       //*** Implement these methods only. ***

       size_t count(const T &data) const;

       bool isDecreasing() const;

       //*** *** *** *** *** *** *** *** ***

   private:

       T* elements;

       size_t _front, _size, _capacity;

       void makeEmpty();

       void init(size_t capacity = 2);

       void copy(const queue& rhs);

       void resize(size_t newCapacity);

   };

   template

   queue::queue() {

       init();

   }

   template

   queue::queue(size_t capacity) {

       init(capacity);

   }

   template

   queue::~queue() {

       makeEmpty();

   }

   template

   queue::queue(const queue &rhs) {

       init(rhs._capacity);

       copy(rhs);

   }

   template

   queue& queue::operator=(const queue& rhs) {

       if (this != &rhs) {

           clear();

           copy(rhs);

       }

       return *this;

   }

   template

   bool queue::operator==(queue& rhs) const {

       if (_size != rhs._size)

           return false;

       size_t size = 0;

       size_t i = _front;

       size_t j = rhs._front;

       while (size++ < _size) {

           if (elements[i] != rhs.elements[j])

               return false;

           i = ((i + 1) % _capacity);

           j = ((j + 1) % rhs._capacity);

       }

       return true;

   }

   template

   bool queue::operator!=(queue& rhs) const {

       return !(this == &rhs);

   }

   template

   void queue::clear() {

       makeEmpty();

       init();

   }

   template

   bool queue::empty() const {

       return _size == 0;

   }

   template

   bool queue::full() const {

       return _size == _capacity;

   }

   template

   size_t queue::size() const {

       return _size;

   }

   template

   void queue::makeEmpty() {

       delete[] elements;

   }

   template

   void queue::init(size_t capacity) {

       _front = _size = 0;

       _capacity = capacity;

       elements = new T[_capacity];

   }

   template

   void queue::copy(const queue& rhs) {

       size_t i = rhs._front;

       while (_size < rhs._size) {

           enqueue(rhs.elements[i]);

           i = ((i + 1) % rhs._capacity);

       }

   }

   template

   void queue::resize(size_t minCapacity) {

       if (minCapacity < _size)

           return;

       if (minCapacity >= 0)

       {

           size_t limit = 1;

           while (limit <= minCapacity)

               limit <<= 1;

           T *tarray = new T[limit];

           for (size_t i = 0; i < _size; i++)

               tarray[i] = elements[(i + _front) % _capacity];

           delete[] elements;

           elements = tarray;

           _capacity = limit;

       }

   }

   template

   void queue::enqueue(const T& val) {

       if (full())

           resize(_size + 1);

       elements[(_front + _size) % _capacity] = val;

       _size++;

   }

   template

   T queue::dequeue() {

       T item = elements[_front];

       _front = (_front + 1) % _capacity;

       _size--;

       return item;

   }

   template

   T queue::peek() const {

       return elements[_front];

   }

   template

   void queue::print() {

       cout << "front=[" << _front << "]" << " ";

       cout << "rear=[" << (_front + _size) % _capacity << "]" << endl;

       if (empty())

       {

           cout << "Empty queue." << endl;

           return;

       }

       size_t i = _front;

       size_t rear = (_front + _size) % _capacity;

       std::cout << " --> ";

       do

       {

           std::cout << "[" << i << "]:" << elements[i];

           i = (i + 1) % _capacity;

           if (i != rear)

               cout << " --> ";

       } while (i != rear);

       std::cout << endl;

   }

   //*** Implement count() and isDecreasing() here. ***

}

QueueMenu.cpp:

// Menu.cpp : Defines the entry point for the console application.

//

#include

#include "Queue.h"

using namespace std;

using namespace cs20a;

enum CHOICE { MAKEEMPTY, ENQUEUE, ISEMPTY, DEQUEUE, GETFRONT, COUNT, DECREASING, QUIT, PRINT, OTHER };

CHOICE menu();

int main(int argc, char* argv[]) {

   int value, total;

   queue queue;

   CHOICE choice;

   do {

       choice = menu();

       switch (choice) {

       case MAKEEMPTY:

           queue.clear();

           break;

       case ISEMPTY:

           if (queue.empty())

               cout << "Queue is empty." << endl;

           else

               cout << "Queue is not empty" << endl;

           break;

       case DEQUEUE:

           if (queue.empty())

               cout << "Queue is empty." << endl;

           else

               cout << "Here's the value on top that got dequeued: " << queue.dequeue() << "." << endl;

           break;

       case GETFRONT:

           if (queue.empty())

               cout << "Queue is empty." << endl;

           else

               cout << "Here's the value on top: " << queue.peek() << endl;

           break;

       case ENQUEUE:

           cout << "Please provide int to enqueue: ";

           cin >> value;

           queue.enqueue(value);

           break;

       case DECREASING:

           if (queue.empty()) {

               cout << "Queue is empty." << endl;

           }

           else if (queue.isDecreasing())

               cout << "queue is decreasing" << endl;

           else

               cout << "queue is not decreasing" << endl;

           break;

       case COUNT:

           cout << "Please provide int to count: ";

           cin >> value;

           total = queue.count(value);

           cout << value << " appears " << total << " times. " << endl;

           break;

       case PRINT:

           queue.print();

           cout << endl;

           break;

       case OTHER:

           cout << "Not a valid choice." << endl;

       }

   } while (choice != QUIT);

   return(0);

}

CHOICE menu() {

   char choice;

   CHOICE result;

   cout << "(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (C)ount Dec(r)easing (P)rint (Q)uit: " << endl;

   cin >> choice;

   switch (choice) {

   case 'E':

   case 'e':

       result = ENQUEUE;

       break;

   case 'M':

   case 'm':

       result = MAKEEMPTY;

       break;

   case 'S':

   case 's':

       result = ISEMPTY;

       break;

   case 'D':

   case 'd':

       result = DEQUEUE;

       break;

   case 'G':

   case 'g':

       result = GETFRONT;

       break;

   case 'Q':

   case 'q':

       result = QUIT;

       break;

   case 'C':

   case 'c':

       result = COUNT;

       break;

   case 'R':

   case 'r':

       result = DECREASING;

       break;

   case 'P':

   case 'p':

       result = PRINT;

       break;

   default:

       result = OTHER;

       break;

   }

   return(result);

}

stack.h:

#pragma once

#include

#include

namespace cs20a

{

   template

   class stack

   {

   public:

       stack();

       explicit stack(size_t capacity);

       stack(const stack &rhs);

       ~stack();

       stack& operator =(const stack & rhs);

       bool operator==(stack & a) const;

       bool operator!=(stack & a) const;

       void push(const T& val);

       T pop();

       T peek() const;

       void clear();

       bool empty() const;

       bool full() const;

       size_t size() const;

       void print();

       //*** Implement these methods only. ***

       bool isConsecutive(const T &data) const;

       void reverse();

       //*** *** *** *** *** *** *** *** ***

   private:

       T *elements;

       size_t _top;

       size_t _capacity;

       void makeEmpty();

       void init(size_t capacity = 2);

       void copy(const stack& rhs);

       void resize(size_t newCapacity);

   };

   template

   stack::stack() {

       init();

   }

   template

   stack::stack(size_t capacity) {

       init(capacity);

   }

   template

   stack::stack(const stack &rhs) {

       init(rhs._capacity);

       copy(rhs);

   }

   template

   stack::~stack()

   {

       makeEmpty();

   }

   template

   stack& stack::operator=(const stack& rhs) {

       if (this != &rhs) {

           clear();

           copy(rhs);

       }

       return *this;

   }

   template

   bool stack::operator==(stack& rhs) const {

       if (_top != rhs._top)

           return false;

       for (size_t i = 0; i < _top; i++)

           if (elements[i] != rhs.elements[i])

               return false;

       return true;

   }

   template

   bool stack::operator!=(stack& rhs) const {

       return !(this == &rhs);

   }

   template

   bool stack::empty() const {

       return (_top == 0);

   }

   template

   bool stack::full() const

   {

       return (_top == _capacity);

   }

   template

   void stack::clear() {

       makeEmpty();

       init();

   }

   template

   size_t stack::size() const {

       return _top;

   }

   template

   void stack::makeEmpty() {

       delete[] elements;

   }

   template

   void stack::init(size_t capacity) {

       _top = 0;

       _capacity = capacity;

       elements = new T[capacity];

   }

   template

   void stack::copy(const stack& rhs) {

       for (size_t i = 0; i < rhs._top; i++)

           push(rhs.elements[i]);

   }

   template

   void stack::resize(size_t minCapacity) {

       if (minCapacity < _top)

           return;

       if (minCapacity >= 0)

       {

           size_t limit = 1;

           while (limit <= minCapacity)

               limit <<= 1;

           T *tarray = new T[limit];

           for (size_t i = 0; i < _top; i++)

               tarray[i] = elements[i];

           delete[] elements;

           elements = tarray;

           _capacity = limit;

       }

   }

   template

   void stack::push(const T& val) {

       if (full())

           resize(_top + 1);

       elements[_top++] = val;

   }

   template

   T stack::pop() {

       return elements[--_top];

   }

   template

   T stack::peek() const {

       return elements[_top - 1];

   }

   template

   void stack::print()

   {

       std::cout << "top=[" << _top << "]" << " " << endl;

       if (empty())

       {

           std::cout << "Empty stack." << endl;

           return;

       }

       std::cout << " --> ";

       for (size_t i = 0; i < _top; i++) {

           std::cout << "[" << i << "]:" << elements[i];

           if (i < (_top - 1))

               cout << " --> ";

       }

       cout << endl;

   }

   //*** Implement isConsecutive() and reverse() here. ***

}

StackMenu.cpp:

// Menu.cpp : Defines the entry point for the console application.

//

#include

#include "stack.h"

using namespace std;

using namespace cs20a;

enum CHOICE { MAKEEMPTY, PUSH, ISEMPTY, POP, TOP, CONSECUTIVE, REVERSE, QUIT, PRINT, OTHER };

CHOICE menu();

int main(int argc, char* argv[]) {

   int value;

   stack stack;

   CHOICE choice;

   do {

       choice = menu();

       switch (choice) {

       case MAKEEMPTY:

           stack.clear();

           break;

       case ISEMPTY:

           if (stack.empty()) {

               cout << "Stack is empty." << endl;

           }

           else {

               cout << "Stack is not empty." << endl;

           }

           break;

       case TOP:

           if (stack.empty())

               cout << "Empty stack." << endl;

           else

               cout << "Value on top: " << stack.pop() << "." << endl;

           break;

       case POP:

           if (stack.empty())

               cout << "Empty stack." << endl;

           else

               cout << "Value on top that got popped: " << stack.pop() << "." << endl;

           break;

       case PUSH:

           cout << "Please provide int to push: ";

           cin >> value;

           stack.push(value);

           break;

       case CONSECUTIVE:

           cout << "Please provide int to check: ";

           cin >> value;

           if (stack.isConsecutive(value)) {

               cout << value << " occurs consecutively." << endl;

           }

           else {

               cout << value << " does not occur consecutively." << endl;

           }

           break;

       case REVERSE:

           if (!stack.empty())

               stack.reverse();

           break;

       case PRINT:

           stack.print();

           cout << endl;

           break;

       case OTHER:

           cout << "Not a valid choice." << endl;

       }

   } while (choice != QUIT);

   return(0);

}

CHOICE menu() {

   char choice;

   CHOICE result;

   cout << "(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit: " << endl;

   cin >> choice;

   switch (choice) {

   case 'M':

   case 'm':

       result = MAKEEMPTY;

       break;

   case 'S':

   case 's':

       result = ISEMPTY;

       break;

   case 'U':

   case 'u':

       result = PUSH;

       break;

   case 'O':

   case 'o':

       result = POP;

       break;

   case 'T':

   case 't':

       result = TOP;

       break;

   case 'Q':

   case 'q':

       result = QUIT;

       break;

   case 'C':

   case 'c':

       result = CONSECUTIVE;

       break;

   case 'R':

   case 'r':

       result = REVERSE;

       break;

   case 'P':

   case 'p':

       result = PRINT;

       break;

   default:

       result = OTHER;

       break;

   }

   return(result);

}

Example: count function Result Queue int> count (5) 3 front->(1) (1) (5) (3) (5) (5) (2) K-back count (10) 0 front- (1) (1) (5) (3) (5) (5) (5) (2) -back

Explanation / Answer

//You can find changes under Changed by chegg EA

//Queue.h

//queue.h:
#pragma once
#include<iostream>
using namespace std;
namespace cs20a
{
   template<typename T>
   class queue {
   public:
       queue();
       explicit queue(size_t capacity);
       queue(const queue &rhs);
       ~queue();
       queue& operator =(const queue & rhs);
       bool operator==(queue & rhs) const;
       bool operator!=(queue & rhs) const;
       void enqueue(const T& val);
       T dequeue();
       T peek() const;
       void clear();
       bool empty() const;
       bool full() const;
       size_t size() const;
       void print();
       //*** Implement these methods only. ***
       size_t count(const T &data) const;
       bool isDecreasing() const;
       //*** *** *** *** *** *** *** *** ***
   private:
       T* elements;
       size_t _front, _size, _capacity;
       void makeEmpty();
       void init(size_t capacity = 2);
       void copy(const queue& rhs);
       void resize(size_t newCapacity);
   };
}

---------------------------------------------------------------------

//Queue.cpp

#include"Queue.h"
using namespace cs20a;
namespace cs20a
{
   template<typename T>
   class queue;
}
template<typename T>
queue<T>::queue() {
   init();
}
template<typename T>
queue<T>::queue(size_t capacity) {
   init(capacity);
}
template<typename T>
queue<T>::~queue() {
   makeEmpty();
}
template<typename T>
queue<T>::queue(const queue<T> &rhs) {
   init(rhs._capacity);
   copy(rhs);
}
template<typename T>
queue<T>& queue<T>::operator=(const queue<T>& rhs) {
   if (this != &rhs) {
       clear();
       copy(rhs);
   }
   return *this;
}
template<typename T>
bool queue<T>::operator==(queue<T>& rhs) const {
   if (_size != rhs._size)
       return false;
   size_t size = 0;
   size_t i = _front;
   size_t j = rhs._front;
   while (size++ < _size) {
       if (elements[i] != rhs.elements[j])
           return false;
       i = ((i + 1) % _capacity);
       j = ((j + 1) % rhs._capacity);
   }
   return true;
}
template<typename T>
bool queue<T>::operator!=(queue<T>& rhs) const {
   return !(this == &rhs);
}
template<typename T>
void queue<T>::clear() {
   makeEmpty();
   init();
}
template<typename T>
bool queue<T>::empty() const {
   return _size == 0;
}
template<typename T>
bool queue<T>::full() const {
   return _size == _capacity;
}
template<typename T>
size_t queue<T>::size() const {
   return _size;
}
template<typename T>
void queue<T>::makeEmpty() {
   delete[] elements;
}
template<typename T>
void queue<T>::init(size_t capacity) {
   _front = _size = 0;
   _capacity = capacity;
   elements = new T[_capacity];
}
template<typename T>
void queue<T>::copy(const queue<T>& rhs) {
   size_t i = rhs._front;
   while (_size < rhs._size) {
       enqueue(rhs.elements[i]);
       i = ((i + 1) % rhs._capacity);
   }
}
template<typename T>
void queue<T>::resize(size_t minCapacity) {
   if (minCapacity < _size)
       return;
   if (minCapacity >= 0)
   {
       size_t limit = 1;
       while (limit <= minCapacity)
           limit <<= 1;
       T *tarray = new T[limit];
       for (size_t i = 0; i < _size; i++)
           tarray[i] = elements[(i + _front) % _capacity];
       delete[] elements;
       elements = tarray;
       _capacity = limit;
   }
}
template<typename T>
void queue<T>::enqueue(const T& val) {
   if (full())
       resize(_size + 1);
   elements[(_front + _size) % _capacity] = val;
   _size++;
}
template<typename T>
T queue<T>::dequeue() {
   T item = elements[_front];
   _front = (_front + 1) % _capacity;
   _size--;
   return item;
}
template<typename T>
T queue<T>::peek() const {
   return elements[_front];
}
template<typename T>
void queue<T>::print() {
   cout << "front=[" << _front << "]" << " ";
   cout << "rear=[" << (_front + _size) % _capacity << "]" << endl;
   if (empty())
   {
       cout << "Empty queue." << endl;
       return;
   }
   size_t i = _front;
   size_t rear = (_front + _size) % _capacity;
   std::cout << " --> ";
   do
   {
       std::cout << "[" << i << "]:" << elements[i];
       i = (i + 1) % _capacity;
       if (i != rear)
           cout << " --> ";
   } while (i != rear);
   std::cout << endl;
}
//*** Implement count() and isDecreasing() here. ***

//added by chegg EA
template<typename T>
bool queue<T>::isDecreasing() const{

   for (i = 0; i < n - 1; i++)
   {
       if (a[i] < a[i + 1])
       {
           return false;
       }
   }
   return true;
}
//Added by chegg EA
template<typename T>
size_t queue<T>::count(const T &data) const
{
   int cnt = 0;
   for (int i = 0; i < _size; i++)
   {
       if (data == elements[i])
           cnt++;
   }
   return cnt;
}

---------------------------------------------

//QueueMenu.cpp

#include<iostream>
#include "Queue.h"
#include"Queue.cpp"
using namespace std;
using namespace cs20a;
enum CHOICE { MAKEEMPTY, ENQUEUE, ISEMPTY, DEQUEUE, GETFRONT, COUNT, DECREASING, QUIT, PRINT, OTHER };
CHOICE menu();
int main(int argc, char* argv[]) {
   int value, total;
   queue<int> queue;
   CHOICE choice;
   do {
       choice = menu();
       switch (choice) {
       case MAKEEMPTY:
           queue.clear();
           //queue.clear();
           break;
       case ISEMPTY:
           if (queue.empty())
               cout << "Queue is empty." << endl;
           else
               cout << "Queue is not empty" << endl;
           break;
       case DEQUEUE:
           if (queue.empty())
               cout << "Queue is empty." << endl;
           else
               cout << "Here's the value on top that got dequeued: " << queue.dequeue() << "." << endl;
           break;
       case GETFRONT:
           if (queue.empty())
               cout << "Queue is empty." << endl;
           else
               cout << "Here's the value on top: " << queue.peek() << endl;
           break;
       case ENQUEUE:
           cout << "Please provide int to enqueue: ";
           cin >> value;
           queue.enqueue(value);
           break;
       case DECREASING:
           if (queue.empty()) {
               cout << "Queue is empty." << endl;
           }
           else if (queue.isDecreasing())
               cout << "queue is decreasing" << endl;
           else
               cout << "queue is not decreasing" << endl;
           break;
       case COUNT:
           cout << "Please provide int to count: ";
           cin >> value;
           total = queue.count(value);
           cout << value << " appears " << total << " times. " << endl;
           break;
       case PRINT:
           queue.print();
           cout << endl;
           break;
       case OTHER:
           cout << "Not a valid choice." << endl;
       }
   } while (choice != QUIT);
   return(0);
}
CHOICE menu() {
   char choice;
   CHOICE result;
   cout << "(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (C)ount Dec(r)easing (P)rint (Q)uit: " << endl;
   cin >> choice;
   switch (choice) {
   case 'E':
   case 'e':
       result = ENQUEUE;
       break;
   case 'M':
   case 'm':
       result = MAKEEMPTY;
       break;
   case 'S':
   case 's':
       result = ISEMPTY;
       break;
   case 'D':
   case 'd':
       result = DEQUEUE;
       break;
   case 'G':
   case 'g':
       result = GETFRONT;
       break;
   case 'Q':
   case 'q':
       result = QUIT;
       break;
   case 'C':
   case 'c':
       result = COUNT;
       break;
   case 'R':
   case 'r':
       result = DECREASING;
       break;
   case 'P':
   case 'p':
       result = PRINT;
       break;
   default:
       result = OTHER;
       break;
   }
   return(result);
}

-------------------------------------------------------

//output, checked Count and isDecreasing methods

(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (C)ount Dec(r)easing (P)rin
t (Q)uit:
E
Please provide int to enqueue: 9
(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (C)ount Dec(r)easing (P)rin
t (Q)uit:
E
Please provide int to enqueue: 6
(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (C)ount Dec(r)easing (P)rin
t (Q)uit:
E
Please provide int to enqueue: 5
(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (C)ount Dec(r)easing (P)rin
t (Q)uit:
E
Please provide int to enqueue: 3
(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (C)ount Dec(r)easing (P)rin
t (Q)uit:
C
Please provide int to count: 3
3 appears 1 times.
(M)akeEmpty I(s)Empty (E)nqueue (D)equeue (G)etFront (C)ount Dec(r)easing (P)rin
t (Q)uit:
r
queue is decreasing

---------------------------------------------------------

//Statck.h

//stack.h:
#pragma once
#include<iostream>
#include<string>
using namespace std;
namespace cs20a
{
   template<typename T>
   class stack
   {
   public:
       stack();
       explicit stack(size_t capacity);
       stack(const stack &rhs);
       ~stack();
       stack& operator =(const stack & rhs);
       bool operator==(stack & a) const;
       bool operator!=(stack & a) const;
       void push(const T& val);
       T pop();
       T peek() const;
       void clear();
       bool empty() const;
       bool full() const;
       size_t size() const;
       void print();
       //*** Implement these methods only. ***
       bool isConsecutive(const T &data) const;
       void reverse();
       //*** *** *** *** *** *** *** *** ***
   private:
       T *elements;
       size_t _top;
       size_t _capacity;
       void makeEmpty();
       void init(size_t capacity = 2);
       void copy(const stack& rhs);
       void resize(size_t newCapacity);
   };
}

-------------------------------

//Stack.cpp

#include"Stack.h"
using namespace cs20a;
namespace cs20a
{
   template<typename T>
   class statck;
}
template<typename T>
stack<T>::stack() {
   init();
}
template<typename T>
stack<T>::stack(size_t capacity) {
   init(capacity);
}
template<typename T>
stack<T>::stack(const stack<T> &rhs) {
   init(rhs._capacity);
   copy(rhs);
}
template<typename T>
stack<T>::~stack()
{
   makeEmpty();
}
template<typename T>
stack<T>& stack<T>::operator=(const stack<T>& rhs) {
   if (this != &rhs) {
       clear();
       copy(rhs);
   }
return *this;
}
template<typename T>
bool stack<T>::operator==(stack<T>& rhs) const {
   if (_top != rhs._top)
       return false;
   for (size_t i = 0; i < _top; i++)
       if (elements[i] != rhs.elements[i])
           return false;
   return true;
}
template<typename T>
bool stack<T>::operator!=(stack& rhs) const {
   return !(this == &rhs);
}
template<typename T>
bool stack<T>::empty() const {
   return (_top == 0);
}
template<typename T>
bool stack<T>::full() const
{
   return (_top == _capacity);
}
template<typename T>
void stack<T>::clear() {
   makeEmpty();
   init();
}
template<typename T>
size_t stack<T>::size() const {
   return _top;
}
template<typename T>
void stack<T>::makeEmpty() {
   delete[] elements;
}
template<typename T>
void stack<T>::init(size_t capacity) {
   _top = 0;
   _capacity = capacity;
   elements = new T[capacity];
}
template<typename T>
void stack<T>::copy(const stack& rhs) {
   for (size_t i = 0; i < rhs._top; i++)
       push(rhs.elements[i]);
}
template<typename T>
void stack<T>::resize(size_t minCapacity) {
   if (minCapacity < _top)
       return;
   if (minCapacity >= 0)
   {
       size_t limit = 1;
       while (limit <= minCapacity)
           limit <<= 1;
       T *tarray = new T[limit];
       for (size_t i = 0; i < _top; i++)
           tarray[i] = elements[i];
       delete[] elements;
       elements = tarray;
       _capacity = limit;
   }
}
template<typename T>
void stack<T>::push(const T& val) {
   if (full())
       resize(_top + 1);
   elements[_top++] = val;
}
template<typename T>
T stack<T>::pop() {
   return elements[--_top];
}
template<typename T>
T stack<T>::peek() const {
   return elements[_top - 1];
}
template<typename T>
void stack<T>::print()
{
   std::cout << "top=[" << _top << "]" << " " << endl;
   if (empty())
   {
       std::cout << "Empty stack." << endl;
       return;
   }
   std::cout << " --> ";
   for (size_t i = 0; i < _top; i++) {
       std::cout << "[" << i << "]:" << elements[i];
       if (i < (_top - 1))
           cout << " --> ";
   }
   cout << endl;
}
//*** Implement isConsecutive() and reverse() here. ***

template<typename T>
bool stack<T>::isConsecutive(const T &data) const
{
   bool status = false;
   for (int i = 0; i < size(); i++)
   {
       if (data == elements[i])
       {
           if (i < size())
           {
               if (elements[i] == elements[i+1])
                   status = true;
           }
       }
   }
   return status;
}

template<typename T>
void stack<T>::reverse()
{
   T *tmp;
   tmp = new T[size()];
  
   //copy elements of original statck to tmp
   int j = 0;
   for (int i = size() - 1; i >=0; i--)
   {
       tmp[j++] = elements[i];
      
   }
   //pop stack
   for (int i = 0; i < size(); i++)
   {
       pop();
   }
   //now add reverse of array to statck
   makeEmpty();
   for (int i = 0 ; i <j; i++)
   {
       push(tmp[i]);
   }
}

-------------------------------------------

//StackMenu.cpp

//StackMenu.cpp:
// Menu.cpp : Defines the entry point for the console application.
//
#include "Stack.h"
#include"Stack.cpp"
using namespace std;
using namespace cs20a;
enum CHOICE { MAKEEMPTY, PUSH, ISEMPTY, POP, TOP, CONSECUTIVE, REVERSE, QUIT, PRINT, OTHER };
CHOICE menu();
int main(int argc, char* argv[]) {
   int value;
   stack<int> stack;
   CHOICE choice;
   do {
       choice = menu();
       switch (choice) {
       case MAKEEMPTY:
           stack.clear();
           break;
       case ISEMPTY:
           if (stack.empty()) {
               cout << "Stack is empty." << endl;
           }
           else {
               cout << "Stack is not empty." << endl;
           }
           break;
       case TOP:
           if (stack.empty())
               cout << "Empty stack." << endl;
           else
               cout << "Value on top: " << stack.pop() << "." << endl;
           break;
       case POP:
           if (stack.empty())
               cout << "Empty stack." << endl;
           else
               cout << "Value on top that got popped: " << stack.pop() << "." << endl;
           break;
       case PUSH:
           cout << "Please provide int to push: ";
           cin >> value;
           stack.push(value);
           break;
       case CONSECUTIVE:
           cout << "Please provide int to check: ";
           cin >> value;
           if (stack.isConsecutive(value)) {
               cout << value << " occurs consecutively." << endl;
           }
           else {
               cout << value << " does not occur consecutively." << endl;
           }
           break;
       case REVERSE:
           if (!stack.empty())
               stack.reverse();
           break;
       case PRINT:
           stack.print();
           cout << endl;
           break;
       case OTHER:
           cout << "Not a valid choice." << endl;
       }
   } while (choice != QUIT);
   return(0);
}
CHOICE menu() {
   char choice;
   CHOICE result;
   cout << "(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit: " << endl;
   cin >> choice;
   switch (choice) {
   case 'M':
   case 'm':
       result = MAKEEMPTY;
       break;
   case 'S':
   case 's':
       result = ISEMPTY;
       break;
   case 'U':
   case 'u':
       result = PUSH;
       break;
   case 'O':
   case 'o':
       result = POP;
       break;
   case 'T':
   case 't':
       result = TOP;
       break;
   case 'Q':
   case 'q':
       result = QUIT;
       break;
   case 'C':
   case 'c':
       result = CONSECUTIVE;
       break;
   case 'R':
   case 'r':
       result = REVERSE;
       break;
   case 'P':
   case 'p':
       result = PRINT;
       break;
   default:
       result = OTHER;
       break;
   }
   return(result);
}

----------------------------------------

//output

(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit:

U
Please provide int to push: 1
(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit:

U
Please provide int to push: 5
(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit:

U
Please provide int to push: 3
(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit:

U
Please provide int to push: 5
(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit:

U
Please provide int to push: 5
(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit:

U
Please provide int to push: 2
(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit:

P
top=[6]
--> [0]:1 --> [1]:5 --> [2]:3 --> [3]:5 --> [4]:5 --> [5]:2

(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit:

C
Please provide int to check: 5
5 occurs consecutively.
(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit:

C
Please provide int to check: 3
3 does not occur consecutively.
(M)akeEmpty I(s)Empty p(U)sh p(O)p (T)op (C)onsecutive (R)everse (P)rint (Q)uit: