# 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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://soumyajit4419.gitbook.io/ds-algo/stack-and-queue/implementation/implementation-of-stack.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
