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

In C++, implement an array-based Stack called ArrayStack which takes an initial

ID: 3884510 • Letter: I

Question

In C++, implement an array-based Stack called ArrayStack which takes an initial capacity for the stack in the constructor. When the stack is full, its size can grow as elements are inserted into the stack. The size of the array should increase by a constant amount . Use the template Stack.h to create ArrayStack using templates for array, filling in all of the member functions. Your code should be able to be complied.

Stack.h

#ifndef ABSTRACT_STACK_H
#define ABSTRACT_STACK_H

template
class AbstractStack
{
private:
// data goes here

public:
AbstractStack(void) {}

~AbstractStack(void) {}

bool isEmpty(void) {}

int size(void) {}

Type top throw(exception) {}

Type pop() throw(exception) {}

void push ( Type e ) {}
};

#endif

Explanation / Answer

Given below is the ArrayBased (expandable) stack implementation. The Stack.h provided in question has a missing #include line, So modified that file as well.

Hope the answer helps. Please don't forget to rate the answer if it helped. Thank you very much.

Stack.h


#ifndef ABSTRACT_STACK_H
#define ABSTRACT_STACK_H
#include <exception>
using namespace std;
template <typename Type>
class AbstractStack
{
private:
// data goes here
public:
AbstractStack(void) {}
~AbstractStack(void) {}
bool isEmpty(void) {}
int size(void) {}
Type top() throw(exception) {}
Type pop() throw(exception) {}
void push ( Type e ) {}
};
#endif

ArrayStack.h


#ifndef ArrayStack_h
#define ArrayStack_h
#include "Stack.h"
template <typename Type>
class ArrayStack : public AbstractStack<Type>
{
private:
Type *elements;
int capacity; //the maximum capacity
int currentSize; // the current number of elements
public:
ArrayStack(int capacity)
{
this->capacity = capacity;
elements = new Type[capacity];
currentSize = 0;
}
~ArrayStack(void)
{
delete []elements;
}
bool isEmpty(void)
{
return currentSize == 0;
}
int size(void)
{
return currentSize;
}
Type top() throw(exception)
{
if(isEmpty())
throw exception();
  
return elements[currentSize - 1];
}
Type pop() throw(exception)
{
if(isEmpty())
throw exception();
  
return elements[--currentSize];

}
void push ( Type e )
{
if(currentSize == capacity)
{
capacity += 10; //add space for 10 more elements
Type *temp = new Type[capacity];
for(int i = 0; i < currentSize; i++)
temp[i] = elements[i];
  
//delete old array
delete []elements;
elements = temp; //assign newly expanded array
  
}
  
elements[currentSize ++] = e;
}
};
#endif /* ArrayStack_h */

TestArrayStack.cpp

#include "ArrayStack.h"
#include <iostream>
using namespace std;
int main()
{
ArrayStack<int> numbers(3); //initial capacity of 3
  
  
//push 10 elements into stack with intial capacity of 3, should expand
for(int i = 1; i <= 10; i++)
numbers.push(2*i);
  
cout << "Top = " << numbers.top() << endl;
cout << "Current Size = " << numbers.size() << endl;
  
cout << "Stack elements are " << endl;
while(!numbers.isEmpty())
cout << numbers.pop() << endl;
  
//now since all elements are popped, trying to access top() should throw exception
try {
cout << "Top = " << numbers.top() << endl;
} catch (exception e) {
cout << "exception occured" << endl;
}
}

output

Top = 20
Current Size = 10
Stack elements are
20
18
16
14
12
10
8
6
4
2
Top = exception occured