Question

In: Computer Science

Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std;...

Implement the following functions for an array based stack.

#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;
};

//--------------------------------------------------------------------
//
//
//  ** Array implementation of the Stack ADT **
//
//--------------------------------------------------------------------
//--------------------------------------------------------------------

template <typename DataType>
StackArray<DataType>::StackArray(int maxNumber)

// Creates an empty stack.

        : maxSize(maxNumber), top(-1)
{
    
}

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

template <typename DataType>
StackArray<DataType>::StackArray(const StackArray& other)

// Copy constructor for  stack

        : maxSize( other.maxSize ), top( other.top )
{
    
}

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

template <typename DataType>
StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other)

// Overloaded assignment operator for the StackArray class.
// Because this function returns a StackArray object reference,
// it allows chained assignment (e.g., stack1 = stack2 = stack3).

{
   return *this;
}

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

template <typename DataType>
StackArray<DataType>::~StackArray()

// Frees the memory used by a stack.

{
    
}

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

template <typename DataType>
void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error)

// Inserts newDataItem onto the top of a stack.

{
   
}

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

template <typename DataType>
DataType StackArray<DataType>::pop() throw (logic_error)

// Removes the topmost data item from a stack and returns it.

{
   
    return dataItems[top];
}

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

template <typename DataType>
void StackArray<DataType>::clear()

// Removes all the data items from a stack.

{
    
}

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

template <typename DataType>
bool StackArray<DataType>::isEmpty() const

// Returns true if a stack is empty. Otherwise, returns false.

{
    return true;
}

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

template <typename DataType>
bool StackArray<DataType>::isFull() const

// Returns true if a stack is full. Otherwise, returns false.

{
    return true;
}

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

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;
}

Solutions

Expert Solution

#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;
};

template <typename DataType>
StackArray<DataType>::StackArray(int maxNumber): maxSize(maxNumber), top(-1)
{
    dataItems=new DataType[maxSize]();
}
template <typename DataType>
StackArray<DataType>::StackArray(const StackArray& other): maxSize( other.maxSize ), top( other.top )
{
    dataItems=new DataType[maxSize];
    for(int i=0;i<maxSize;i++)
        dataItems[i]=other.dataItems[i];
}

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

template <typename DataType>
StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other)
{
        this.maxSize=other.maxSize;
        this.top=other.top;
        this.dataItems=new DataType[maxSize];
    for(int i=0;i<maxSize;i++)
        this.dataItems[i]=other.dataItems[i];
   return *this;
}


template <typename DataType>
StackArray<DataType>::~StackArray(){
    delete []dataItems;
    top=-1;
    maxSize=0;
}


template <typename DataType>
void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error){
        if(isFull())
                return;
        top=top+1;
   dataItems[top]=newDataItem;
}


template <typename DataType>
DataType StackArray<DataType>::pop() throw (logic_error){
        if(isEmpty())
                return 0;
        top=top-1;
    return dataItems[top+1];
}


template <typename DataType>
void StackArray<DataType>::clear(){
    top=-1;
}


template <typename DataType>
bool StackArray<DataType>::isEmpty() const{
        if(top==-1)
        return true;
    return false;
}

template <typename DataType>
bool StackArray<DataType>::isFull() const{
        if(top+1==maxSize)
        return true;
    return false;
}


template <typename DataType>
void StackArray<DataType>::showStructure() const{
    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;
}

Related Solutions

Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an odd positive integer. The program reads a value (n) entered by the user and prints an n x n grid displaying a large letter X. The left half should be made up of pluses (+) and the right half should be made with the character "x" and the very center should be an asteric...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an integer between 1 and 20. If the user enters an illegal number, the program repeatedly asks the user to enter the correct one. If the user has not entered a correct number after 10 attempts, the program chooses the number 10 as the user's number. The program prints the cube of the user's number.
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
Do an insertion sort based off this code #include <iostream> #define MAX_INT 2147483647 using namespace std;...
Do an insertion sort based off this code #include <iostream> #define MAX_INT 2147483647 using namespace std;    int main(int argc,char **argv) { int* Sequence; int arraySize = 1; // Get the size of the sequence cin >> arraySize; Sequence = new int[arraySize];    // Read the sequence for(int i=0; i<arraySize; i++) cin >> Sequence[i];
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT