Question

I'm having some trouble in my Data Structures (cop 4530) class. I need to "Implement a generic Stack container as an adaptor class template". My template and the implementation are in two different files stack.h and stack.hpp (this is required). I wanted to do this Stack set up as an Array, but it seems as though my teacher set it us up to do it as a linked list if I'm not wrong?? I'm just confused on how to get started setting up the stack as an Array if someone could explain it to me (we have a very useless book). And all I really need is someone to explain to me just a few of the functions from ".hpp" where I implement my Stack code. Particularly the copy / move functions. (If it's better or easier for me to do it a different way than I'm trying then please share)

Here's Stack.h; These are all interfaces of the "Stack class Template" we needed to include

#include <iostream>
#define MAX_SIZE 100

namespace cop4530{

template<typename T>
class Stack{

 private:
 int A[MAX_SIZE];
 int top;

 public:

 Stack();
 ~Stack();
 Stack (const Stack<T>&);
 Stack(Stack<T> &&);
 Stack<T>& operator= (const Stack <T>&);
 Stack<T> & operator=(Stack<T> &&);
 bool empty() const;
 void clear();
 void push(const T& x);
 void push(T && x);
 void pop();
 T& top();
 const T& top() const;
 int size() const;
 void print(std::ostream& os, char ofc = ' ') const;  

}

 //non-member global functions
 std::ostream& operator<< (std::ostream& os, const Stack<T>& a);
 bool operator== (const Stack<T>&, const Stack <T>&);
 bool operator!= (const Stack<T>&, const Stack <T>&);
 bool operator< (const Stack<T>& a, const Stack <T>& b); 

 #include "Stack.hpp"

}

#endif

And here's the separate "Stack.hpp" file that holds the implementation of some of them.

#include "stack.h"
#include <iostream>

namespace cop4530{

template<typename T>
Stack<T>::Stack(){
//zero argument constructor
}

template<typename T>
Stack<T>::~Stack(){ //destructor
    clear();
}

template<typename T>
Stack<T>::Stack(const Stack<T>&){ //copy constructor

}

template<typename T>
Stack<T>::Stack(Stack<T> &&){ //move constructor

}

template<typename T>
Stack<T>::Stack<T> & Stack<T>::operator=(const Stack<T> &){
    //copy assignment operator=
}

template<typename T>
Stack<T>::Stack<T> & Stack<T>::operator=(Stack<T> &&){
    //move assignment operator=
}

/*does this look right? I don't think so...*/
template<typename T>
void Stack<T>::push(const T& x){ //adds x to the stack, copy version
    insert(begin(), x);
}

template<typename T>
void Stack<T>::push(T && x){ // adds x to the stack, move version
    insert(begin(), std::move(val));
}
Was it helpful?

Solution

To implement the Stack as a adapter class for array you should really start from the basic functionality of a stack. Let's consider a few basics:

  • constructor- created an empty stack

  • isFull/isEmpty - return true/false depending on how much space is available in your stack.

  • push - adds one element at the top of the queue

  • pop - removes the first element from the top of the queue

Let's start with the constructor: you could say that your stack is empty when top == -1. Since your array is statically allocated (you can consider to dynamically allocate memory for the array in the constructor), all you need to do is top =-1 and you have yourself an empty stack.

isFull/isEmpty now become obvious:

  • isEmpty: return top == -1

  • isFull: return top == MAX_SIZE

PUSH: adds one elements to the top of the stack:

    if (!isFull)
    {
       top++;
       A[top] = new_element;
    }

I'll let you figure out the rest; the idea is to update the stack pointer top and always keep an eye on how much space you have available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top