HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

C++ Stack Implementation Using Templates and Linked List

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
stacktemplatesusingandimplementationlistlinked

Problem

I have a simple stack class. It uses a linked list for its data structure and it is of type template so any datatype (including pointers) can be passed in. It can be initialized on the stack or on the heap. It does only what a stack should do and nothing more.

It is constructed entirely in the header file to avoid compiler errors for missing types(I believe that's standard procedure for template). I created it with Xcode on a mac and have not tested it on Linux or Windows yet for compiler errors. The code is well commented and warns the caller they are responsible for deletion of heap allocated objects(just like a C++ vector would be).
I want to discuss this topic in a blog or something so I want to make sure it is correct. Please review my code for completeness and correctness

My Stack:

```
#ifndef TStack_h
#define TStack_h

#include
#include

template class TStack{

public:

//####################################
// Constructor.
//####################################

TStack();

//####################################
// Destructor.
//####################################

~TStack();

//####################################
// Class methods.
//####################################

/**
* Adds an item to the stack.
* Notes:
*   N/A
* ------
* Arguments:
*   template: the type of the class.
* ------
* Return:
*   N/A
* ------
* Throws
*   N/A
*/
void push(T elem);

/**
* Removes the data item at the beginning of the stack.
* Notes:
*   Caller is responsible for releasing objects that are popped from the stack.
* ------
* Arguments:
*   N/A
* ------
* Return:
*  dataType T: the type
* ------
* Throws
*   out_of_range exception for an empty stack.
*/
T pop();

/**
* The size of the stack.
* Notes:
*  N/A
* ------
* Arguments:
*   N/A
* ------
* Return:
*  int : The size of the stack.
* ------
* Throws
*   N/A
*/
int getSize();

/**
* Reports if the stack is emp

Solution

If you have a destructor you should also define the copy constructor and copy assign + the move variants. Otherwise the compiler will generate its own (incorrect) copy and move facilities which will lead to dangling pointers and double frees.

When pushing the element is copied. You should also provide a move variant of push:

template void TStack::push(T&& elem){

    Node * newNode = new Node();
    newNode->data_ = std::move(elem);
    newNode->next_ = NULL;

    // If the head is NULL just assign it to newNode();
    if(this->head_ == NULL){
        this->head_= newNode;
    }else{
        newNode->next_ = this->head_;
        this->head_ = newNode;
    }

    this->size_ += 1;
}


As an aesthetic point you can be a bit more frugal with the whitespace. Too much whitespace spreads out the relevant details too much which can make it harder to read.

Code Snippets

template<class T> void TStack< T >::push(T&& elem){

    Node * newNode = new Node();
    newNode->data_ = std::move(elem);
    newNode->next_ = NULL;

    // If the head is NULL just assign it to newNode();
    if(this->head_ == NULL){
        this->head_= newNode;
    }else{
        newNode->next_ = this->head_;
        this->head_ = newNode;
    }

    this->size_ += 1;
}

Context

StackExchange Code Review Q#153106, answer score: 10

Revisions (0)

No revisions yet.