C++ Programming. I already have the rest of the code written out just copy and p
ID: 3869920 • Letter: C
Question
C++ Programming. I already have the rest of the code written out just copy and paste the header and source files below into a project and complete the 2
"To Do" tasks. Please make sure both parts are fully answered and that you upload copyable code for each cpp or header file that was edited. Make sure it runs and compiles.
To Do: implement the Stack ADT (50 points) using array based approach
implement the Stack ADT (50 points) using the linked list approach
Files:
StackArray.cpp:
#include "StackArray.h"
template <typename DataType>
StackArray<DataType>::StackArray(int maxNumber)
{
}
template <typename DataType>
StackArray<DataType>::StackArray(const StackArray& other)
{
}
template <typename DataType>
StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other)
{
}
template <typename DataType>
StackArray<DataType>::~StackArray()
{
}
template <typename DataType>
void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error)
{
}
template <typename DataType>
DataType StackArray<DataType>::pop() throw (logic_error)
{
}
template <typename DataType>
void StackArray<DataType>::clear()
{
}
template <typename DataType>
bool StackArray<DataType>::isEmpty() const
{
return false;
}
template <typename DataType>
bool StackArray<DataType>::isFull() const
{
return false;
}
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 << " ";
cout << endl;
for ( j = 0 ; j <= top ; j++ )
{
if( j == top )
{
cout << '[' << dataItems[j] << ']'<< " "; // Identify top
}
else
{
cout << dataItems[j] << " ";
}
}
cout << endl;
}
cout << endl;
}
"StackLinked.cpp"
#include "StackLinked.h"
template <typename DataType>
StackLinked<DataType>::StackLinked (int maxNumber)
{
}
template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
}
template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{
}
template <typename DataType>
StackLinked<DataType>::~StackLinked()
{
clear();
}
template <typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error)
{
}
template <typename DataType>
DataType StackLinked<DataType>::pop() throw (logic_error)
{
}
template <typename DataType>
void StackLinked<DataType>::clear()
{
StackNode* t;
while ( top != NULL)
{
t = top;
top = top->next;
delete t;
}
}
template <typename DataType>
bool StackLinked<DataType>::isEmpty() const
{
return false;
}
template <typename DataType>
bool StackLinked<DataType>::isFull() const
{
return false;
}
template <typename DataType>
void StackLinked<DataType>::showStructure() const
{
if( isEmpty() )
{
cout << "Empty stack" << endl;
}
else
{
cout << "Top ";
for (StackNode* temp = top; temp != 0; temp = temp->next) {
if( temp == top ) {
cout << "[" << temp->dataItem << "] ";
}
else {
cout << temp->dataItem << " ";
}
}
cout << "Bottom" << endl;
}
}
Explanation / Answer
StackArray.h
==============
#include <iostream>
template<typename DataType>
class StackArray
{
unsigned int top, maxSize;
DataType* Array;
public:
StackArray(int maxNumber);
StackArray(const StackArray& other);
void push(const DataType& newDataItem) throw (std::logic_error);
DataType pop() throw(std::logic_error);
void clear();
bool isEmpty() const;
bool isFull() const;
void showStructure() const;
StackArray<DataType>& operator=(const StackArray& other);
~StackArray();
};
=======================
StackArray.cc
=======================
#include "StackArray.h"
template <typename DataType>
StackArray<DataType>::StackArray(int maxNumber)
{
top = 0;
maxSize = maxNumber;
Array = new DataType[maxSize];
}
template <typename DataType>
StackArray<DataType>::StackArray(const StackArray& other)
{
top = 0;
maxSize = 1000;
Array = new DataType[maxSize];
DataType temp;
while(temp = other.pop())
Array[top++] = temp;
}
template <typename DataType>
StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other)
{
}
template <typename DataType>
StackArray<DataType>::~StackArray()
{
delete [] Array;
Array = NULL;
}
template <typename DataType>
void StackArray<DataType>::push(const DataType& newDataItem) throw (std::logic_error)
{
if(top==maxSize)
{
throw std::logic_error("Overflow. ");
}
Array[top++] = newDataItem;
}
template <typename DataType>
DataType StackArray<DataType>::pop() throw (std::logic_error)
{
if(top==0)
{
throw std::logic_error("Underflow.");
}
DataType value = Array[--top];
return value;
}
template <typename DataType>
void StackArray<DataType>::clear()
{
top=0;
}
template <typename DataType>
bool StackArray<DataType>::isEmpty() const
{
if(top==0) return true;
return false;
}
template <typename DataType>
bool StackArray<DataType>::isFull() const
{
if(top == maxSize) return true;
return false;
}
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()) std::cout << "Empty stack." << std::endl;
else {
int j;
std::cout << "Top = " << top-1 << std::endl;
for(j = 0 ; j < maxSize ; j++ )
std::cout << j << " ";
std::cout << std::endl;
for ( j = 0 ; j < top ; j++ )
{
if( j == top-1 )
std::cout << '[' << Array[j] << ']'<< " "; // Identify top
else std::cout << Array[j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
===========================
StackLinked.h
============================
#include <iostream>
template<typename DataType>
class StackLinked
{
unsigned int count, maxSize;
struct StackNode
{
DataType data;
struct StackNode* next;
};
struct StackNode* top;
public:
StackLinked(int maxNumber);
StackLinked(const StackLinked& other);
void push(const DataType& newDataItem) throw (std::logic_error);
DataType pop() throw(std::logic_error);
void clear();
bool isEmpty() const;
bool isFull() const;
void showStructure() const;
StackLinked<DataType>& operator=(const StackLinked& other);
~StackLinked();
};
===============================
StackLinked.cc
===============================
#include "StackLinked.h"
template <typename DataType>
StackLinked<DataType>::StackLinked (int maxNumber)
{
count = 0;
maxSize = maxNumber;
top = NULL;
}
template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
count = 0;
maxSize = 10000;
DataType temp;
while(temp = other.pop())
{
struct StackNode *newNode = new (StackNode);
newNode->next = top;
newNode->data = temp;
top++;
}
}
template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{
}
template <typename DataType>
StackLinked<DataType>::~StackLinked()
{
clear();
}
template <typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (std::logic_error)
{
if(count==maxSize) throw std::logic_error("Overflow");
StackNode* temp = new StackNode;
temp->data = newDataItem;
temp->next = top;
top = temp;
count++;
}
template <typename DataType>
DataType StackLinked<DataType>::pop() throw (std::logic_error)
{
if(count == 0) throw std::logic_error("Underflow");
DataType t_data = top->data;
StackNode* temp = top;
top = top->next;
delete temp;
count--;
return t_data;
}
template <typename DataType>
void StackLinked<DataType>::clear()
{
StackNode* t;
while ( top != NULL)
{
t = top;
top = top->next;
delete t;
}
}
template <typename DataType>
bool StackLinked<DataType>::isEmpty() const
{
if(count == 0) return true;
return false;
}
template <typename DataType>
bool StackLinked<DataType>::isFull() const
{
if(count == maxSize) return true;
return false;
}
template <typename DataType>
void StackLinked<DataType>::showStructure() const
{
if(isEmpty()) std::cout << "Empty stack" << std::endl;
else
{
std::cout << "Top ";
for(StackNode* temp = top; temp != NULL; temp = temp->next)
{
if( temp == top ) std::cout << "[" << temp->data << "] ";
else std::cout << temp->data << " ";
}
}
std::cout << "Bottom" << std::endl;
}
==============================
main.cc
==============================
#include "StackLinked.cc"
#include "StackArray.cc"
int main(int argc, char const *argv[])
{
StackArray<int> name(16);
if(name.isEmpty()) std::cout << "Empty ";
name.push(112);
name.push(2323);
name.push(234234);
name.push(3);
name.push(343);
name.push(33);
name.push(545);
name.push(676);
if(!name.isFull()) std::cout << "Not full ";
if(!name.isEmpty()) std::cout << "Not Empty ";
name.showStructure();
name.clear();
name.showStructure();
if(name.isEmpty()) std::cout << "Empty ";
StackLinked<int> namelinked(16);
if(namelinked.isEmpty()) std::cout << "Empty ";
namelinked.push(112);
namelinked.push(2323);
namelinked.push(234234);
namelinked.push(3);
namelinked.push(343);
namelinked.push(33);
namelinked.push(545);
namelinked.push(676);
if(!namelinked.isFull()) std::cout << "Not full ";
if(!namelinked.isEmpty()) std::cout << "Not Empty ";
namelinked.showStructure();
namelinked.clear();
namelinked.showStructure();
if(namelinked.isEmpty()) std::cout << "Empty ";
return 0;
}
==================================
run using
g++ main.cc -o main && ./main
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
=====================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.