Stack

Requirements:

In this sub-project, you will implement one class:

  1. Stack: StackAsArray.

A stack stores elements in an ordered list and allows insertions and deletions at one end in O(1) time.

The elements in this stack are stored in an array.

Runtime:

The run time of each member function is specified in parentheses at the end of the description.

Class Specifications:


StackAsArray

Description

A class which implements a stack using an array. The size of the array is fixed at initialization time. For run-time requirements, the number of elements in the queue is n. The array is considered to be full if all entries in the array are assigned.

Member Variables

The class at least three members:

  • A pointer to an instance of Type, Type *array, to be used as an array,
  • A top index int itop, and
  • The size of the array, int array_size.

Member Functions

Constructors

StackAsArray( int n = 10 )

The constructor takes as an argument the size of the array and allocates memory for that array. The default number of entries is 10. Other class members are assigned as appropriate.

Destructor

~StackAsArray()

The destructor deletes the memory allocated for the array.

Accessors

This class has four accessors:

Type top() const
Return the object at the top of the stack. This may throw a underflow exception). (O(1))
int size() const
Returns the number of elements currently stored in the stack. (O(1))
bool empty() const
Returns true if the stack is empty, false otherwise. (O(1))
bool full() const
Returns true if the array is full and false otherwise. (O(1))

Mutators

This class has three mutators:

void push( Type const & )
Insert the new element at the top of the stack. This may throw a overflow exception. (O(1))
Type pop()
Removes the element at the top of the stack. This may throw a underflow exception. (O(1))
void clear()
Removes all the elements in the stack. (O(1))

Friends

The class has no friends.