Stack Implementation in C
Tags: c, stack, algorithms
Published: 2020-12-25 18:02:57

### We need these header files...
```c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
```

### Let's define some constants...
```c
// constants
#define MAX 5              // Max capacity of a stack
#define ERR "\x1B[31m"     // Error Color
#define SUCCESS "\x1B[32m" // Success Color
#define RESET "\033[0m"    // Reset Color
```
### Constants defined in above lines of code such as `ERR`,`SUCCESS` and `RESET` will be used to color the text of `printf()`. `MAX` defines the maximum capacity of the stack 
### Now we'll declare a stack...
```c
//declaration of stack
typedef struct stack
{
    int entry[MAX];
    int top;
} stack;
```

### Initialization of stack
```c
//stack initialization
void init(stack *sptr)
{
    sptr->top = 0;
}
```
### A function to check if Stack is empty...
```c
//checking stack is empty or not.
int isEmpty(stack *sptr)
{
    if (sptr->top == 0)
        return (1);
    else
        return (0);
}
```
### A function to check if stack is overflowed...
```c
//checking stack is full or not.
int isFull(stack *sptr)
{
    if (sptr->top == MAX)
        return (1);
    else
        return (0);
}
```
### A function to push/insert a new element into the stack...
```c
//Inserting element into stack
void push(stack *sptr)
{
    int item;
    if (isFull(sptr))
        printf(ERR "\tSTACK IS FULL" RESET);
    else
    {
        printf("\n\tEnter the element: ");
        scanf("%d", &item);
        sptr->entry[sptr->top] = item;
        sptr->top++;
    }
}
```
### A function to pop/remove elements from the stack...
```c
//Deleting element from the stack
void pop(stack *sptr)
{
    int item;

    if (isEmpty(sptr))
        printf(ERR "\tSTACK IS EMPTY" RESET);
    else
    {
        sptr->top--;
        item = (sptr->entry[sptr->top]);
        printf(SUCCESS "\t%d HAS BEEN REMOVED" RESET, item);
    }
}
```

### A function to traverse though the stack or print all the elements of the stack
```c
//Traversing the elements of stack
void traverse(stack *sptr)
{
    if (isEmpty(sptr))
        printf(ERR "\tSTACK IS EMPTY" RESET);
    else
    {
        int i;

        for (i = sptr->top - 1; i >= 0; i--)
            printf("\t-> %d\n", sptr->entry[i]);
    }
}
```

### And finally the `main()` function...
```c
int main()
{
    stack s;
    int choice;

    init(&s);

    do
    {
        printf("\n 1. PUSH");
        printf("\n 2. POP");
        printf("\n 3. TRAVERSE");
        printf("\n 4. EXIT");
        printf("\n\nEnter the choice: ");
        scanf(" %d", &choice);
        switch (choice)
        {
        case 1:
            push(&s);
            break;
        case 2:
            pop(&s);
            break;
        case 3:
            traverse(&s);
            break;
        case 4:
            exit(0);
            break;
        default:
            printf(ERR "\tWRONG CHOICE" RESET);
        }
    } while (1);

    return 0;
    getch();
}
``` 

### Output

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zewltvvtkva39y2pn33j.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zwh3v96nlr3bq2xjz13n.png)

### Pushed 5 elements to the stack. `(max capacity=5)`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cfjygsi50xf93l123sxp.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0l9qhmp4u4vhehpuf5x7.png)

### Removed two elements from the stack
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pt67d3bmmmyqkibyxsf4.png)

### Removed all elements until the stack is empty
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xcvg5f2y24yu11krll6k.png)

{% youtube -isvC8juMUo %}

### Enjoy :)