0% found this document useful (0 votes)
59 views1 page

DynamicStack Class Implementation in C++

The document defines a DynamicStack class template that implements a stack using a List. The DynamicStack class contains methods to check if the stack is empty, get the size, push items onto the stack, pop items off the stack, and access the top item. It uses a List object to store the stack elements and implements each stack method by calling corresponding List methods.

Uploaded by

Blake Howe
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views1 page

DynamicStack Class Implementation in C++

The document defines a DynamicStack class template that implements a stack using a List. The DynamicStack class contains methods to check if the stack is empty, get the size, push items onto the stack, pop items off the stack, and access the top item. It uses a List object to store the stack elements and implements each stack method by calling corresponding List methods.

Uploaded by

Blake Howe
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

...ures and Patterns\Assignment 6\Resources\DynamicStack.

h
#pragma once
#include "..\..\Resources\ListPS6.h"
#include <stdexcept>
template<class T>
class DynamicStack
{
private:
List<T> fElements;
public:
bool isEmpty() const;
int size() const;
void push(const T& aItem);
void pop();
const T& top() const;
};
template<class T>
bool DynamicStack<T>::isEmpty() const
{
return (size()==0);
}
template<class T>
int DynamicStack<T>::size() const
{
return [Link]();
}
template<class T>
void DynamicStack<T>::push(const T & aItem)
{
fElements.push_front(aItem);
}
template<class T>
void DynamicStack<T>::pop()
{
if (!isEmpty()) {
//remove the last appended item
[Link](fElements[0]);
}
else {
throw std::logic_error("No values in the stack to perform a pop");
}
}
template<class T>
const T & DynamicStack<T>::top() const
{
return fElements[0];
}

You might also like