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

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...
--- 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...
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];
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT