# Stack Using Linked List

### Stack Node Structure :

```c
struct Node {
    int data;
    struct Node *next;
};

struct Node *top = NULL;
```

### Function to Check if Stack is Empty :

```c
int isEmpty() {
    return top == NULL;
} 
```

### Stack Push Function :

```c
void push(int data) {

    // Creating a new stack node
    struct Node *newNode;
    newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode -> data = data;
    
    // Set the next to point to current top
    newNode -> next = top;
    
    // Update top
    top = newNode;

}
```

### Stack Pop Function :

```c
int pop() {

    if (isEmpty()) {
        printf("Stack underflow !\n");
        return -1;
    }
    
    struct Node *toDelete = top;
    int poppedValue = top -> data;
    top = top -> next;
    
    free(toDelete);
    return poppedValue;

}
```

### Stack Peek Function :

```c
int peek() {
    return top -> data;
}
```

### Stack Display Function :

```c
void display() {

    if (isEmpty()) {
        printf("Stack is empty\n");
        return;
    }
    
    struct Node *ptr = top;
    while (ptr != NULL) {
        printf("%d\t", ptr -> data);
        ptr = ptr -> next;
    }
    printf("\n");

}
```

Contributed by Nitin Ranganath


---

# 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://nitinranganath.gitbook.io/data-structures/stack/stack-using-linked-list.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.
