C only Stack ADT using array based impletation To write stac

C++ only

Stack ADT using array based impletation

To write stackarrary.cpp follow the structure below:

// show6.cpp

//--------------------------------------------------------------------

template <typename DataType>
void StackArray<DataType>::showStructure() const

// Array implementation. Outputs the data items in a stack. If the
// stack is empty, outputs \"Empty stack\". This operation is intended
// for testing and debugging purposes only.

{
if( isEmpty() ) {
   cout << \"Empty stack.\" << endl;
}
else {
   int j;
   cout << \"Top = \" << top << endl;
   for ( j = 0 ; j < maxSize ; j++ )
   cout << j << \"\\t\";
   cout << endl;
   for ( j = 0 ; j <= top ; j++ )
   {
   if( j == top )
   {
   cout << \'[\' << dataItems[j] << \']\'<< \"\\t\"; // Identify top
   }
   else
   {
       cout << dataItems[j] << \"\\t\";
   }
   }
   cout << endl;
}
cout << endl;
}

//--------------------------------------------------------------------

template <typename DataType>
void StackLinked<DataType>::showStructure() const

// Linked list implementation. Outputs the data elements in a stack.
// If the stack is empty, outputs \"Empty stack\". This operation is
// intended for testing and debugging purposes only.

{
if( isEmpty() )
{
   cout << \"Empty stack\" << endl;
}
else
{
cout << \"Top\\t\";
   for (StackNode* temp = top; temp != 0; temp = temp->next) {
   if( temp == top ) {
       cout << \'[\" << temp->dataItem << \"]\\t\";
   }
   else {
       cout << temp->dataItem << \"\\t\";
   }
   }
cout << \"Bottom\" << endl;
}

}

and Stackarray.h is


#ifndef STACKARRAY_H
#define STACKARRAY_H

#include <stdexcept>
#include <iostream>

using namespace std;

#include \"Stack.h\"

template <typename DataType>
class StackArray : public Stack<DataType> {
public:
StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE);
StackArray(const StackArray& other);
StackArray& operator=(const StackArray& other);
~StackArray();

void push(const DataType& newDataItem) throw (logic_error);
DataType pop() throw (logic_error);

void clear();

bool isEmpty() const;
bool isFull() const;

void showStructure() const;

private:
int maxSize;
int top;
DataType* dataItems;
};

#endif       //#ifndef STACKARRAY_H

Write in C++ only

i will really appreciate it.

Thank you

Array-Based Implementation Step 1: Implement the operations in the Stack ADT using an array to store the stack data items. Stacks change in size, therefore you need to store the maximum number of data items the stack can hold (maxsize) and the array index of the topmost data item in the stack (top), along with the stack data items themselves (data Items). Base your implementation on the declarations from the file StackArray h. An implementation of the showstructure operation is given in the file show6.cpp. Step 2: Save your array implementation of the Stack ADT in the file StackArray.cpp. Be sure to document your code.

Solution

test6.cpp


#include <iostream>

using namespace std;

#include \"config.h\"

#if !LAB6_TEST1
# include \"StackArray.cpp\"
#else
# include \"StackLinked.cpp\"
#endif

void print_help()
{
    cout << endl << \"Commands:\" << endl;
    cout << \" H : Help (displays this message)\" << endl;
    cout << \" +x : Push x\" << endl;
    cout << \" - : Pop\" << endl;
    cout << \" C : Clear\" << endl;
    cout << \" E : Empty stack?\" << endl;
    cout << \" F : Full stack?\" << endl;
    cout << \" Q : Quit the test program\" << endl;
    cout << endl;
}

template <typename DataType>
void test_stack(Stack<DataType>& testStack)
{
    DataType testDataItem;            // Stack data item
    char cmd;                         // Input command

    print_help();

    do
    {
        testStack.showStructure();                    // Output stack

        cout << endl << \"Command: \";                  // Read command
        cin >> cmd;
        if ( cmd == \'+\' )
           cin >> testDataItem;

   try {
        switch ( cmd )
        {
          case \'H\' : case \'h\':
           print_help();
           break;

          case \'+\' :                                  // push
           cout << \"Push \" << testDataItem << endl;
           testStack.push(testDataItem);
           break;

          case \'-\' :                                  // pop
           cout << \"Popped \" << testStack.pop() << endl;
           break;

          case \'C\' : case \'c\' :                       // clear
           cout << \"Clear the stack\" << endl;
           testStack.clear();
           break;

          case \'E\' : case \'e\' :                       // isEmpty
           if ( testStack.isEmpty() )
              cout << \"Stack is empty\" << endl;
           else
              cout << \"Stack is NOT empty\" << endl;
           break;

          case \'F\' : case \'f\' :                       // isFull
           if ( testStack.isFull() )
              cout << \"Stack is full\" << endl;
           else
              cout << \"Stack is NOT full\" << endl;
           break;

          case \'Q\' : case \'q\' :                   // Quit test program
           break;

          default :                               // Invalid command
           cout << \"Inactive or invalid command\" << endl;
        }
   }
   catch (logic_error e) {
        cout << \"Error: \" << e.what() << endl;
   }

    } while ( cin && cmd != \'Q\' && cmd != \'q\' );
}

int main() {
#if !LAB6_TEST1
    cout << \"Testing array implementation\" << endl;
    StackArray<char> s1;
    test_stack(s1);
#else
    cout << \"Testing linked implementation\" << endl;
    StackLinked<char> s2;
    test_stack(s2);
#endif
}


StackArray.h


#ifndef STACKARRAY_H
#define STACKARRAY_H

#include <stdexcept>
#include <iostream>

using namespace std;

#include \"Stack.h\"

template <typename DataType>
class StackArray : public Stack<DataType> {
public:
    StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE);
    StackArray(const StackArray& other);
    StackArray& operator=(const StackArray& other);
    ~StackArray();

    void push(const DataType& newDataItem) throw (logic_error);
    DataType pop() throw (logic_error);

    void clear();

    bool isEmpty() const;
    bool isFull() const;

    void showStructure() const;

private:
    int maxSize;
    int top;
    DataType* dataItems;
};

#endif       //#ifndef STACKARRAY_H

StackLinked.h


#ifndef STACKARRAY_H
#define STACKARRAY_H

#include <stdexcept>
#include <iostream>

using namespace std;

#include \"Stack.h\"

template <typename DataType>
class StackLinked : public Stack<DataType> {

public:

    StackLinked(int maxNumber = Stack<DataType>::MAX_STACK_SIZE);
    StackLinked(const StackLinked& other);
    StackLinked& operator=(const StackLinked& other);
    ~StackLinked();

    void push(const DataType& newDataItem) throw (logic_error);
    DataType pop() throw (logic_error);

    void clear();

    bool isEmpty() const;
    bool isFull() const;

    void showStructure() const;

private:

    class StackNode {
      public:
   StackNode(const DataType& nodeData, StackNode* nextPtr);

   DataType dataItem;
   StackNode* next;
    };

    StackNode* top;
};

#endif       //#ifndef STACKARRAY_H

StackLinked.h


#ifndef STACKARRAY_H
#define STACKARRAY_H

#include <stdexcept>
#include <iostream>

using namespace std;

#include \"Stack.h\"

template <typename DataType>
class StackLinked : public Stack<DataType> {

public:

    StackLinked(int maxNumber = Stack<DataType>::MAX_STACK_SIZE);
    StackLinked(const StackLinked& other);
    StackLinked& operator=(const StackLinked& other);
    ~StackLinked();

    void push(const DataType& newDataItem) throw (logic_error);
    DataType pop() throw (logic_error);

    void clear();

    bool isEmpty() const;
    bool isFull() const;

    void showStructure() const;

private:

    class StackNode {
      public:
   StackNode(const DataType& nodeData, StackNode* nextPtr);

   DataType dataItem;
   StackNode* next;
    };

    StackNode* top;
};

#endif       //#ifndef STACKARRAY_H

config.h

#define LAB6_TEST1   0   // 0 => use array implementation, 1 => use linked impl.

show6.cpp

// show6.cpp

//--------------------------------------------------------------------

template <typename DataType>
void StackArray<DataType>::showStructure() const

// Array implementation. Outputs the data items in a stack. If the
// stack is empty, outputs \"Empty stack\". This operation is intended
// for testing and debugging purposes only.

{
    if( isEmpty() ) {
   cout << \"Empty stack.\" << endl;
    }
    else {
   int j;
   cout << \"Top = \" << top << endl;
   for ( j = 0 ; j < maxSize ; j++ )
        cout << j << \"\\t\";
   cout << endl;
   for ( j = 0 ; j <= top ; j++ )
   {
        if( j == top )
        {
            cout << \'[\' << dataItems[j] << \']\'<< \"\\t\"; // Identify top
        }
        else
        {
       cout << dataItems[j] << \"\\t\";
        }
   }
   cout << endl;
    }
    cout << endl;
}

//--------------------------------------------------------------------

template <typename DataType>
void StackLinked<DataType>::showStructure() const

// Linked list implementation. Outputs the data elements in a stack.
// If the stack is empty, outputs \"Empty stack\". This operation is
// intended for testing and debugging purposes only.

{
    if( isEmpty() )
    {
   cout << \"Empty stack\" << endl;
    }
    else
    {
        cout << \"Top\\t\";
   for (StackNode* temp = top; temp != 0; temp = temp->next) {
        if( temp == top ) {
       cout << \'[\' << temp->dataItem << \']\'<<\\t\";
        }
        else {
       cout << temp->dataItem << \"\\t\";
        }
   }
        cout << \"Bottom\" << endl;
    }

}

Stack.h


#ifndef STACK_H
#define STACK_H

#include <stdexcept>
#include <iostream>

using namespace std;

template <typename DataType>
class Stack {
public:
    static const int MAX_STACK_SIZE = 8;

    virtual ~Stack();

    virtual void push(const DataType& newDataItem) throw (logic_error) = 0;
    virtual DataType pop() throw (logic_error) = 0;

    virtual void clear() = 0;

    virtual bool isEmpty() const = 0;
    virtual bool isFull() const = 0;

    virtual void showStructure() const = 0;
};

template <typename DataType>
Stack<DataType>::~Stack()
// Not worth having a separate class implementation file for the destuctor
{}

#endif       // #ifndef STACK_H

C++ only Stack ADT using array based impletation To write stackarrary.cpp follow the structure below: // show6.cpp //-------------------------------------------
C++ only Stack ADT using array based impletation To write stackarrary.cpp follow the structure below: // show6.cpp //-------------------------------------------
C++ only Stack ADT using array based impletation To write stackarrary.cpp follow the structure below: // show6.cpp //-------------------------------------------
C++ only Stack ADT using array based impletation To write stackarrary.cpp follow the structure below: // show6.cpp //-------------------------------------------
C++ only Stack ADT using array based impletation To write stackarrary.cpp follow the structure below: // show6.cpp //-------------------------------------------
C++ only Stack ADT using array based impletation To write stackarrary.cpp follow the structure below: // show6.cpp //-------------------------------------------
C++ only Stack ADT using array based impletation To write stackarrary.cpp follow the structure below: // show6.cpp //-------------------------------------------
C++ only Stack ADT using array based impletation To write stackarrary.cpp follow the structure below: // show6.cpp //-------------------------------------------

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site