Could really use some help. Thank you in advance! Objectives: The main objective
ID: 3718994 • Letter: C
Question
Could really use some help. Thank you in advance!
Objectives: The main objectives of this project are to test your ability to create and use stack- based dynamic data structures, and generalize your code using templates. A review of your knowledge to manipulate dynamic memory, classes, pointers and iostream to all extents, is also included. You may from now on freely use square bracket-indexing, pointers, references, all operators, the library, and the std::string type as you deem proper Description: For this project you will create a templated Stack class, with an Array-based and a Node-based variant. A Stack is a Last In First Out (LIFO) data structure. A Stack exclusively inserts data at the top (push) and removes data from the top (pop) as well. The Stack's m top data member is used to keep track of the current Stack size, and through that also infer the position of the last inserted element (the most recent one) The following provided specifications refer to Stacks that work with DataType class objects similarly to the previous project. For the this Project's requirements, you will have to make the necessary modifications so that your ArrayStack and NodeStack and all their functionalities are generalized templated classes. Templated Array-based Stack: The following header file excerpt is used to explain the required specifications for the class. This only refers an Array-based Stack that holds elements of type class DataType. You have to template this class, and provide the necessary header file with the necessary declarations and implementations const size t MAX_STACKSIZE 1000; class ArrayStack friend std: :ostream &operator;Explanation / Answer
//ArrayStack.h
#pragma once
#include<iostream>
#ifndef ARRAY_STACK_H
#define ARRAY_STACK_H
const size_t MAX_STACKSIZE = 1000;
template<typename DataType>
class ArrayStack
{
friend std::ostream & operator<<(std::ostream &os, const ArrayStack & arrayStack);
public:
ArrayStack();
ArrayStack(size_t count, const DataType &value);
ArrayStack(const ArrayStack & other);
~ArrayStack();
ArrayStack &operator=(const ArrayStack & other);
DataType &top();
const DataType &top() const;
void push(const DataType &value);
void pop();
size_t size() const;
bool empty() const;
bool full() const;
void clear();
void serialize(std::ostream &os) const;
private:
DataType m_container[MAX_STACKSIZE];
size_t top;
};
template<typename DataType>
ArrayStack<DataType>::ArrayStack()
{
top = -1;
}
template<typename DataType>
inline ArrayStack<DataType>::ArrayStack(size_t count, const DataType & value)
{
top = -1;
for (int i = 0; i < count; i++)
{
m_container[++top] = value;
}
}
template<typename DataType>
inline ArrayStack<DataType>::ArrayStack(const ArrayStack & other)
{
this->top = other.top;
this->m_container = new DataType[top];
for (int i = 0; i < top; i++)
{
this->m_container[i] = other.m_container[i];
}
}
template<typename DataType>
ArrayStack<template<typename DataType>>::~ArrayStack()
{
}
template<typename DataType>
inline ArrayStack & ArrayStack<DataType>::operator=(const ArrayStack & other)
{
// TODO: insert return statement here
if (this == &other)
return *this;
this->top = other.top;
this->m_container = new DataType[top];
for (int i = 0; i < top; i++)
{
this->m_container[i] = other.m_container[i];
}
return *this;
}
template<typename DataType>
inline DataType & ArrayStack<DataType>::top()
{
// TODO: insert return statement here
if (!empty())
{
return this->m_container[this->top];
}
}
template<typename DataType>
inline const DataType & ArrayStack<DataType>::top() const
{
// TODO: insert return statement here
}
template<typename DataType>
inline void ArrayStack<DataType>::push(const DataType & value)
{
if (!full())
{
this->m_container[++(this->top)] = value;;
}
}
template<typename DataType>
inline void ArrayStack<DataType>::pop()
{
if (!empty())
{
(this->top)--;
}
}
template<typename DataType>
inline size_t ArrayStack<DataType>::size() const
{
return (this->top) -1;
}
template<typename DataType>
inline bool ArrayStack<DataType>::empty() const
{
return (this->top) == -1;;
}
template<typename DataType>
inline bool ArrayStack<DataType>::full() const
{
return (this->top) == MAX_STACKSIZE;
}
template<typename DataType>
inline void ArrayStack<DataType>::clear()
{
(this->top) = -1;
delete this->m_container;
}
template<typename DataType>
inline void ArrayStack<DataType>::serialize(std::ostream & os) const
{
for (int i = 0; i < this->top; i++)
{
os << this->m_container[i]
}
}
#endif // !ARRAY_STACK_H
//NodeStack.h
#pragma once
#ifndef NODE_STACK_H
#define NODE_STACK_H
#include<iostream>
template<typename DataType>
class Node
{
friend class NodeStack;
public:
Node();
Node(const DataType &data, Node *next=NULL);
DataType *data;
const DataType &data() const;
private:
Node * m_next;
DataType m_data;
};
template<typename DataType>
class NodeStack
{
friend std::ostream & operator<<(std::ostream &os, const NodeStack & nodeStack);
public:
NodeStack();
NodeStack(size_t count, const DataType &value);
NodeStack(const NodeStack & other);
~NodeStack();
NodeStack &operator=(const NodeStack & other);
DataType &top();
const DataType &top() const;
void push(const DataType &value);
void pop();
size_t size() const;
bool empty() const;
bool full() const;
void clear();
void serialize(std::ostream &os) const;
private:
Node * m_top;
};
template<typename DataType>
NodeStack<DataType>::NodeStack()
{
m_top = NULL;
}
template<typename DataType>
inline NodeStack<DataType>::NodeStack(size_t count, const DataType & value)
{
m_top = NULL;
for (int i = 0; i < count; i++)
{
Node *n = new Node(value);
if (m_top == NULL)
{
m_top = n;
}
else
{
n->m_next = m_top;
m_top = n;
}
}
}
template<typename DataType>
inline NodeStack<DataType>::NodeStack(const NodeStack & other)
{
Node *curr = m_top;
other.m_top = NULL;
while (curr != NULL)
{
Node *n = new Node(curr->m_data);
if (other.m_top == NULL)
{
other.m_top = n;
}
else
{
n->m_next = other.m_top;
other.m_top = n;
}
}
}
template<typename DataType>
NodeStack<DataType>::~NodeStack()
{
}
template<typename DataType>
inline NodeStack & NodeStack<DataType>::operator=(const NodeStack & other)
{
if (this == &other)
return *this;
// TODO: insert return statement here
Node *curr = m_top;
other.m_top = NULL;
while (curr != NULL)
{
Node *n = new Node(curr->m_data);
if (other.m_top == NULL)
{
other.m_top = n;
}
else
{
n->m_next = other.m_top;
other.m_top = n;
}
}
return *this;
}
template<typename DataType>
inline DataType & NodeStack<DataType>::top()
{
// TODO: insert return statement here
if (!empty())
{
return this->m_top->m_data;
}
}
template<typename DataType>
inline const DataType & NodeStack<DataType>::top() const
{
// TODO: insert return statement here
if (!empty())
{
return this->m_top->m_data;
}
}
template<typename DataType>
inline void NodeStack<DataType>::push(const DataType & value)
{
Node *n = new Node(value);
if (m_top == NULL)
{
m_top = n;
}
else
{
n->m_next = m_top;
m_top = n;
}
}
template<typename DataType>
inline void NodeStack<DataType>::pop()
{
if (!empty())
{
m_top = m_top->m_next;
}
}
template<typename DataType>
inline size_t NodeStack<DataType>::size() const
{
int count = 0;
Node *curr = m_top;
while (curr)
{
count++;
curr = curr->m_next;
}
return count;
}
template<typename DataType>
inline bool NodeStack<DataType>::empty() const
{
return m_top == NULL;
}
template<typename DataType>
inline bool NodeStack<DataType>::full() const
{
return false;
}
template<typename DataType>
inline void NodeStack<DataType>::clear()
{
Node *curr = m_top;
Node *n;
while (curr)
{
n = curr;
delete n;
curr = curr->m_next;
}
m_top = NULL;
}
template<typename DataType>
inline void NodeStack<DataType>::serialize(std::ostream & os) const
{
Node *curr = m_top;
while (curr)
{
os << curr = curr->m_next.data << " ";
}
}
#endif // !NODE_STACK_H
template<typename DataType>
inline Node<DataType>::Node()
{
}
template<typename DataType>
inline Node<DataType>::Node(const DataType & data, Node * next=NULL)
{
Node *n = new Node();
n->m_data = data;
n->m_next = next;
}
template<typename DataType>
inline const DataType & Node<DataType>::data() const
{
// TODO: insert return statement here
return this->m_data;
}
//ArrayStack.cpp
#include "ArrayStack.h"
std::ostream & operator<<(std::ostream & os, const ArrayStack<int> & arrayStack)
{
// TODO: insert return statement here
for (int i = 0; i < arrayStack.top; i++)
{
os << arrayStack.m_container[i]
}
}
//NodeStack.cpp
#include "NodeStack.h"
std::ostream & operator<<(std::ostream & os, const NodeStack<int> & nodeStack)
{
// TODO: insert return statement here
Node *curr = nodeStack.m_top;
while (curr)
{
os << curr = curr->m_next.data << " ";
}
}