DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

How to implement own stack in c++

C++ implementation own stack:

Implementation of stack can be done in many ways but I have implemented using Array and Linked list with template using C++.

Source code with Array:

/*
Program: Stack 
Author: Alpha Master
Date: 27 Nov 2021
*/

//Header File
#include<iostream>
#include<string.h>

//max size
const static int gSize = 10;

template<class T>
class Stack
{

	T mArray[gSize];
	int mTop;
	public:
	Stack()
	{
		mTop = -1;
	}
	~Stack()
	{
		mTop = -1;
	}

	//Push function
	void push(T val)
	{
		//check its full or not
		if(mTop == gSize -1)
		{
		std::cout<<"Stack is full"<<std::endl;
		return;
		}

		//add the element
		mTop++;
		mArray[mTop] = val;
	}

	//top function
	T top()
	{
		return mArray[mTop];
	}

	//pop function
	void pop()
	{
		if(mTop == -1)
		{
		std::cout<<"Stack is empty"<<std::endl;
		return;
		}
		//remove the element
		--mTop;
		return;
	}

	//empty
	bool isEmpty()
	{
		if(mTop == -1)
		return true;
		else
		return false;
	}

};

int main()
{
	std::cout<<"Stack"<<std::endl;
	Stack<int> obj;
	obj.push(1);
        obj.push(2);
        obj.push(3);
        obj.push(4);
        obj.push(5);
	
	while(!(obj.isEmpty()))
	{
		std::cout<<obj.top()<<" ";
		obj.pop();
	}
	std::cout<<" "<<std::endl;

	//stack with float
	Stack<float>obj1;
        obj1.push(6.3);
        obj1.push(7.3);
        obj1.push(8.2);
        obj1.push(9.1);
        obj1.push(10.5);

        while(!(obj1.isEmpty()))
        {
                std::cout<<obj1.top()<<" ";
                obj1.pop();
        }
        std::cout<<" "<<std::endl;


	std::cout<<"Bye"<<std::endl;

	return 0;
}

Output:

Stack
5 4 3 2 1  
10.5 9.1 8.2 7.3 6.3  
Bye

Source code with linked list:

/*
Program: Stack with linklist 
Author: Alpha Master
Date: 27 Nov 2021
*/

//Header File
#include<iostream>

template<class T>
class Stack
{
	struct node
	{
		T data;
		node* next;
	} *mTop;
	public:
	Stack() {mTop = nullptr;}
	~Stack();
	void push(T val);
	T top();
	void pop();
	bool isEmpty();
};

template<class T>
Stack<T>::~Stack()
{
	if(mTop == nullptr)
	return;

	node* tmp = nullptr;
	while(mTop != nullptr)
	{
		tmp = mTop;
		mTop = mTop->next;
		delete tmp;
	}

}

template<class T>
void Stack<T>::push(T val)
{
	node* tmp = new node;
	if(tmp == nullptr)
	{
		std::cout<<"Stack is full"<<std::endl;
		return;
	}
	
	tmp->data = val;
	tmp->next = mTop;
	mTop = tmp;
}

template<class T>
T Stack<T>::top()
{
	return (mTop->data);
}

template<class T>
void Stack<T>::pop()
{
	if(mTop == nullptr)
	return;
	node* tmp = mTop;
	mTop = mTop->next;
	delete tmp;
	return;
}

template<class T>
bool Stack<T>::isEmpty()
{
	if(mTop == nullptr)
	return true;
	else
	return false;
}

int main()
{
	std::cout<<"Stack with Link list"<<std::endl;
        Stack<int> obj;
        obj.push(1);
        obj.push(2);
        obj.push(3);
        obj.push(4);
        obj.push(5);

        while(!(obj.isEmpty()))
        {
                std::cout<<obj.top()<<" ";
                obj.pop();
        }
        std::cout<<" "<<std::endl;

        //stack with float
        Stack<float>obj1;
        obj1.push(6.3);
        obj1.push(7.3);
        obj1.push(8.2);
        obj1.push(9.1);
        obj1.push(10.5);

        while(!(obj1.isEmpty()))
        {
                std::cout<<obj1.top()<<" ";
                obj1.pop();
        }
        std::cout<<" "<<std::endl;


        std::cout<<"Bye"<<std::endl;

	return 0;
}

Output:

Stack with Link list
5 4 3 2 1  
10.5 9.1 8.2 7.3 6.3  
Bye

Primary Sidebar

DigestCPP © 2022. All rights reserved.

    Contact Us   Disclaimer