Pages

Cteate Stack in C++ using template

Stack is a data structure that allows access to only one data item at a time. If you remove this data item from the stack you can access to the next to last data item of the stack. Also stack perform a Last-In-First-Out(LIFO) operation on the stack. You may find more on stack here.

I will explain basic stack operations and then let's move to code.

Push - Insert a data item to the top of the stack.

Pop - Remove a data item from the top the stack.

Peek - Retrieve the data item from the top of the stack.

Other stack operations can be understand by just looking at the code simply.




This is the stack created by using template of C++. So that you can set any data or class type for stack data. Further more this post will answer the question, How to create template stack in C++?

The Stack.h source file is,

#include <stdlib.h>

#ifndef STACK_H_
#define STACK_H_

template<class T> class Stack{

private:
    unsigned int maxSize;
    T *stackData;
    int top;

public:
    Stack(int size){
        stackData = new T[size];//to hold the T ‌type data items
        top = -1;//no items on the stack
        maxSize = size;//set maximum size that stack can hold
    }


    virtual ~Stack(){}

    int count(){
        return top + 1;
    }


    bool isEmpty(){
        return top == -1 ? true : false;
    }


    bool isFull(){
        return top == maxSize - 1 ? true : false;
    }


    T* peek(){
        if(!isEmpty())//check for empty
            return &stackData[top - 1];
    }


    T* pop(){
        if(!isEmpty()){
            top -= 1;//decrease the top by 1 to indicate the delete
            return &stackData[top];//return deleted item
        }
        return NULL;
    }


    void push(T* item){
        stackData[top++] = *item;//insert to data array and increase the top by one
    }
};


#endif /* STACK_H_ */

Then the demonstration code,

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

using namespace std;

int main() {

    int data;
    Stack<int> intStack(10);//create integer stack with maximum 10 items

    //insert data to stack
    data = 1;
    intStack.push(&data);
    data = 2;
    intStack.push(&data);
    data = 3;
    intStack.push(&data);
    data = 5;
    intStack.push(&data);

    //get top item
    cout << *intStack.peek() << endl;

    intStack.pop();
    intStack.pop();

    //get top item
    cout << *intStack.peek() << endl;
   
    return 0;
}


I expect this will help anybody who want to create stack with the template!

1 comment:

  1. Hi Milinda Aruna,
    Thanks for your post. I'm a novice in C/C++. I had try your code with VS2010. It run OK. But I saw at line:

    stackData = new T[size];//to hold the T ‌type data items

    You had new a pointer object but I don't see you do any thing in the destroy function. I had tried to to put addtion code like this.

    virtual ~Stack(){
    if(stackData){
    delete stackData;
    cout<<"~Stack: Destroyed"<<endl;
    }
    };

    I run again, and get error. Could you explain what is issue when I do like this. Many thanks!

    ReplyDelete