# Stack Using Array

### Stack Structure :

```c
struct Stack {
    int size;
    int top;
    int *data;
};
```

### Creating a Stack Dynamically :

```c
struct Stack *createStack(int size) {

    // Allocating memory for stack
    struct Stack *stack;
    stack = (struct Stack *)malloc(sizeof(struct Stack));

    // Setting size and top
    stack -> size = size;
    stack -> top = -1;

    // Allocating memory for data array
    stack -> data = (int *)malloc(size * sizeof(int));

    return stack;
    
}
```

### Function to Check if Stack is Empty or Full :

```c
int isFull(struct Stack *stack) {
    return stack -> top == stack -> size - 1;
}

int isEmpty(struct Stack *stack) {
    return stack -> top == -1;
}
```

### Stack Push Function :

```c
void push(struct Stack *stack, int data) {

    if (isFull(stack)) {
        printf("Stack overflow !\n");
        return;
    } else {
        stack -> top++;
        stack -> data[stack -> top] = data;
    }
    
}
```

### Stack Pop Function :

```c
int pop(struct Stack *stack) {

    if (isEmpty(stack)) {
        printf("Stack underflow !\n");
        return -1;
    } else {
        int poppedData = stack -> data[stack -> top];
        stack -> top--;
        return poppedData;
    }
    
}
```

### Stack Peek Function :

```c
int peek(struct Stack *stack) {

    if (isEmpty(stack)) {
        return -1;
    }
    return stack -> data[stack -> top];

}
```

### Stack Display Function :

```c
void display(struct Stack *stack) {

    if (isEmpty(stack)) {
        printf("Stack is empty\n");
        return;
    }
    
    for (int i = stack -> top; i >= 0; i--) {
        printf("%d\n", stack -> data[i]);
    }

}
```

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-array.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.
