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

Data Structures using C++ Use the ArrayStackType class below: #ifndef ARRAYSTACK

ID: 3864699 • Letter: D

Question

Data Structures using C++

Use the ArrayStackType class below:

#ifndef ARRAYSTACKTYPE_H

#define ARRAYSTACKTYPE_H

#include <iostream>

#include <new>        // for bad_alloc exception

#include <cstdlib>    // for the exit function

#include "StackADT.h"

using namespace std;

const int MAX_STACK_SIZE = 100; // Maximum number of components

template <class ItemType>

class ArrayStackType: public StackADT<ItemType>

{

public:

       // Constructor

       ArrayStackType();

          // Default constructor.

          // Post: An empty stack has been created. The variable list holds

          //       the base address of the array (i.e., the address for the

          //       bottom element of the array), stackTop = 0.

       ArrayStackType(const ArrayStackType<ItemType>& otherStack);

          // Copy constructor for deep copy of the array-based stacks.

       // Destructor

       ~ArrayStackType();

          // Delete all the stack elements.

          // Post: The array (list) holding the stack elements is deleted.

       // Action responsibilities

       void resetStack();

          // Reset the stack to an empty stack.

          // Post: Stack is empty.

       void push(const ItemType& newItem);

          // Function to add newItem to the stack.

          // Pre: The stack exists and is not full.

          // Post: The stack is changed and newItem is added to the

          //       top of the stack.

       void pop();

          // Function to remove the top element of the stack.

          // Pre: The stack exists and is not empty.

          // Post: The stack is changed and the top element is removed from

          //       the stack.

       // Knowledge responsibilities

       ItemType top() const;

          // Function to return the top element of the stack.

          // Pre: The stack exists and is not empty.

          // Post: If the stack is empty, the program terminates; otherwise,

          //       the top element of the stack is returned.

       bool isEmpty() const;

          // Post: Returns true if stack is empty; false otherwise.

       bool isFull() const;

          // Post: Returns true if there is room for another item;

          //       false otherwise.

       // Operator overloading

       const ArrayStackType<ItemType>& operator=

                     (const ArrayStackType<ItemType>& otherStack);

          // Overload the assignment operator.

protected:

       ItemType *list; // Pointer to the array that holds the stack items

       int stackTop; // The index of the top item in the array plus 1

                     // e.g., If there is only one item in stack, stackTop=1.

private:

       void copyStack(const ArrayStackType<ItemType>& otherStack);

          // Function to make a copy of otherStack.

          // A deep copy of otherStack is created and assigned to this Stack.

};

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

template <class ItemType>

ArrayStackType<ItemType>::ArrayStackType()

{

       try

       {

              list = new ItemType[MAX_STACK_SIZE];

       }// end try

       catch (bad_alloc)

       {

              cout << "ERROR: Cannot allocate memory! ";

              exit(EXIT_FAILURE);

       }

       stackTop = 0;

}

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

template <class ItemType>

ArrayStackType<ItemType>::ArrayStackType

                (const ArrayStackType<ItemType>& otherStack)

{

       try

       {

              list = new ItemType[MAX_STACK_SIZE];

       }// end try

       catch (bad_alloc)

       {

              cout << "ERROR: Cannot allocate memory! ";

              exit(EXIT_FAILURE);

       }

       copyStack(otherStack);

}

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

template <class ItemType>

ArrayStackType<ItemType>::~ArrayStackType()

{

       delete [] list; // Deallocate the memory occupied by the array.

}

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

template <class ItemType>

void ArrayStackType<ItemType>::resetStack()

{

       stackTop = 0;

}

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

template <class ItemType>

void ArrayStackType<ItemType>::push(const ItemType& newItem)

{

       if ( !isFull() )

       {

              list[stackTop] = newItem; // add newItem at the top

             stackTop++; // increment stackTop

       }

       else

              cout << "Cannot add to a full stack." << endl;

}

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

template <class ItemType>

void ArrayStackType<ItemType>::pop()

{

       if ( !isEmpty() )

              stackTop--; // decrement stackTop

       else

              cout << "Cannot remove from an empty stack." << endl;

}

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

template <class ItemType>

ItemType ArrayStackType<ItemType>::top() const

{

       if ( !isEmpty() )

              return list[stackTop - 1]; // the top item is indexed

                                     // by stackTop - 1

       else // if stack is empty, terminate the program

       {

              cout << "ERROR: Cannot return top item from an empty stack! ";

              exit(EXIT_FAILURE);

       }

}

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

template <class ItemType>

bool ArrayStackType<ItemType>::isEmpty() const

{

       return (stackTop == 0);

}

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

template <class ItemType>

bool ArrayStackType<ItemType>::isFull() const

{

       return ( stackTop == MAX_STACK_SIZE );

}

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

template <class ItemType>

const ArrayStackType<ItemType>& ArrayStackType<ItemType>::operator=

                    (const ArrayStackType<ItemType>& otherStack)

{

       if ( this != &otherStack ) // avoid self-copying

              copyStack(otherStack);

       return *this;

}

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

template <class ItemType>

void ArrayStackType<ItemType>::copyStack

                    (const ArrayStackType<ItemType>& otherStack)

{

       stackTop = otherStack.stackTop;

       for (int i = 0; i < stackTop; i++)

              list[i] = otherStack.list[i];

}

#endif

Consider the following statements:

ArrayStackType<int> stack;

int x;

Suppose that the input is:

16 45 34 23 12 5 -999

Without actually executing the program in Visual C++, what do you expect the output of the following code segment to be?

cin >> x;

while ( x != -999 )

{

       if ( x % 3 != 0 )

       {

              if ( !stack.isFull() )

                     stack.push(x);

       }

       else

              cout << "x = " << x << endl;

       cin >> x;

}

cout << "Stack Items: ";

while ( !stack.isEmpty() )

{

       cout << " " << stack.top();

       stack.pop();

}

cout << endl;

Explanation / Answer

first all the input which is %3 !=0 print and other will pust into stack.
After element pop from stack.

OUTPUT :

First While Loop cout print.

x = 45
x = 12

In second While Loop cout print.
x = 5
x = 23
x = 34
x = 16

Final Output

x = 45
x = 12
x = 5
x = 23
x = 34
x = 16