> For the complete documentation index, see [llms.txt](https://soumyajit4419.gitbook.io/ds-algo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soumyajit4419.gitbook.io/ds-algo/stack-and-queue/implementation/implementation-of-stack.md).

# Implementation of Stack

## Implement Stack using LL

<pre class="language-cpp"><code class="lang-cpp">struct StackNode
{
    int data;
    StackNode *next;
    StackNode(int a)
    {
        data = a;
        next = NULL;
    }
};

struct MyStack
{
    StackNode *top;

    void push(int);
    int pop();
    MyStack() { top = NULL; }
};

<strong>
</strong><strong>
</strong><strong>//Function to push an integer into the stack.
</strong>void MyStack ::push(int x)
{
    // Your Code
    StackNode *p = new StackNode(x);

    if (!top)
    {
        top = p;
    }
    else
    {
        StackNode *temp = NULL;
        temp = top;
        top = p;
        p->next = temp;
    }
}

// Function to remove an item from top of the stack.
int MyStack ::pop()
{
    // Your Code

    if (!top)
    {
        return -1;
    }

    int x = top->data;

    top = top->next;

    return x;
}
</code></pre>
