Featured Post

Trie implementation in C

Stack Implementation in C++

A Stack is a data structure which works on the principle LIFO(Last In, First Out).The basic operations in a Stack are :

1. Push : In which we push the data into the stack.
2. Pop : In which we remove an element from the Stack.

All insertions and removals are done only from one side of the Stack , which is called the 'top' of the Stack.

A stack is generally used in function calls where the local variables are pushed onto the Stack and when the function returns , it pops the variables from the Stack.

This is a simple implementation of Stack in C++.

#include <iostream>

using namespace std;

class Stack
{
private:
    int *p;
    int top,length;

public:
    Stack(int = 0);
    ~Stack();

    void push(int);
    int pop();
    void display();
};

Stack::Stack(int size)
{
    top=-1;
    length=size;
    if(size == 0)
        p = 0;
    else
        p=new int[length];
}

Stack::~Stack()
{
    if(p!=0)
        delete [] p;
}

void Stack::push(int elem)
{
    if(p == 0)                //If the stack size is zero, allow user to mention it at runtime
    {
        cout<<"Stack of zero size"<<endl;
        cout<<"Enter a size for stack : ";
        cin >> length;
        p=new int[length];
    }
    if(top==(length-1))     //If the top reaches to the maximum stack size
    {
        cout<<"\nCannot push "<<elem<<", Stack full"<<endl;
        return;
    }
    else
    {
        top++;
        p[top]=elem;
    }
}
int Stack::pop()
{
    if(p==0 || top==-1)
    {
        cout<<"Stack empty!";
        return -1;
    }
    int ret=p[top];
    top--;
    return ret;
}

void Stack::display()
{
    for(int i = 0; i <= top; i++)
        cout<<p[i]<<" ";
    cout<<endl;
}

int main()
{
    Stack s1;             //We are creating a stack of size 'zero'
    s1.push(1);
    s1.display();
    s1.push(2);
    s1.push(3);
    s1.push(4);
    s1.push(5);
    s1.display();
    s1.pop();
    s1.display();
    s1.pop();
    s1.display();
    s1.pop();
    s1.display();
    s1.pop();
    s1.display();
}

Comments

  1. Good and simple implementation! :)
    You can make use of templates to make the stack class as a generic one. Small improvement though. :)

    ReplyDelete
  2. i would like to know whether the method display() does conform to the basic format or basic structure of stacks, does it print in reverse of the order of input?

    ReplyDelete
  3. It prints in the bottom-top order. Its not in the conventional manner.

    ReplyDelete
    Replies
    1. just change for loop set int i = top; i >0; i--

      Delete
  4. Thank you so so much. This helped me immensely.

    ReplyDelete
  5. inside the pop function why you do "length --;"

    ReplyDelete
  6. Thanks bro, it helps me a lot. You can also visit my blog, and please tell me what better for my blog.
    http://www.mycodingland.com/

    ReplyDelete
  7. how the hell are we supposed to scroll through ur damn page!!!
    remove the fking sidebar from the right
    good post anyways

    ReplyDelete

Post a Comment

Please post your valuable suggestions