Stack with LinkedList

void push(int x)
{
        StackNode *tmp=new StackNode;
        tmp->data=x;
        tmp->next=top;
        top=tmp;
}

/*The method pop which return the element poped out of the stack*/
int pop()
{
        int v;
        StackNode *q;
        if(top!=NULL)
        {
            q=top;
            v=top->data;
            top=top->next;
            delete(q);
            return v;
        }
        else
        return -1;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *