Thursday, December 19, 2013

Dynamic Stack implementation

#pragma once

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

template<class Stack_type>
class Dynamic_stack
{
    struct node
    {
     Stack_type stack_element;
     node* next;
    }*top;
public:
    Dynamic_stack(void);
    bool push_element(Stack_type);
    bool pop_element();
    bool is_empty();
    int no_of_element();
    void print_element();
   
public:
    ~Dynamic_stack(void);
};

//constructor
template<class Stack_type>
Dynamic_stack<Stack_type>::Dynamic_stack(void)
{
    top = 0;
}

//destructor
template<class Stack_type>
Dynamic_stack<Stack_type>::~Dynamic_stack(void)
{

}
template<class Stack_type>
bool Dynamic_stack<Stack_type>::is_empty()
{
    return top == NULL ? 1:0;
}

template <class Stack_type>
bool Dynamic_stack<Stack_type>::push_element(Stack_type element)
{
    node* new_node = new node();
    new_node->stack_element = element;

    if(is_empty())
    {
        top = new_node;
        new_node->next = NULL;
        return true;
   
    }
    else
    {
        new_node->next = top;
        top = new_node;
        return true;
    }
}
template<class Stack_type>
int Dynamic_stack<Stack_type>::no_of_element()
{
    int count = 0;
    node* temp_node = top;
    while(temp_node)
    {
        count++;
        temp_node = temp_node->next;
    }
    return count;

}
template <class Stack_type>
void Dynamic_stack<Stack_type>:: print_element()
{
    node* temp_node = top;
    if(temp_node == NULL)
    {
        printf("Sorry there is no element to display in the stack");
    }
    else
    {
        while(temp_node)
        {
            printf("%d \n", temp_node->stack_element);
            temp_node = temp_node->next;

        }
    }
   
}

template<class Stack_type>
bool Dynamic_stack<Stack_type>::pop_element()
{
    //Stack is following LIFO(Last in first out) structure, so top element has to popped up
    //top has to updated with next of top.

    if(is_empty())
    {
        printf("Stack is empty\n");
        return false;
    }
    else
    {
        node* temp_node = top;
        printf("Popped Elemenet is: %d \n",temp_node->stack_element);
        temp_node  = temp_node->next;
        top = temp_node;
        return true;
    }

}

int main()
{

Dynamic_stack<int> int_dynamic_stack;
Dynamic_stack<char> char_dynamic_stack;
int element = 10;
char c_element = 'A';
int_dynamic_stack.push_element(element);
int_dynamic_stack.push_element(element+5);
int_dynamic_stack.push_element(element+6);

printf("\nTotal number of element: %d \n",int_dynamic_stack.no_of_element());

printf("After adding element in the stack:\n");
int_dynamic_stack.print_element();

int_dynamic_stack.pop_element();
printf("After Pop element in the stack:\n");
int_dynamic_stack.print_element();

int_dynamic_stack.pop_element();
printf("After 2Pop element in the stack:\n");
int_dynamic_stack.print_element();

int_dynamic_stack.pop_element();
printf("After 3 Pop element in the stack:\n");
int_dynamic_stack.print_element();

}
output:

No comments: