Create Stack Using Array In C

Create Stack Using Array In C

Stacks: It is a linear data structure. Collection with access only to the last element inserted.


Create Stack Using Array In C


  • Last In First Out (LIFO)
  • Insert/push
  • Remove/Pop
  • Top
  • Make empty


A stack element can be implemented by both an array and a linked list.


Code here 👇


#include<stdio.h>
#include<stdlib.h>
#define MAX 4
int top = -1;
int stack[MAX];


void push(int a)
{
    if(top == MAX - 1){
        printf("Stack is full");
}
    else{
        top++;
        stack[top]=a;
}
        
}


int pop(int a)
{
    if(top==-1)
        printf("Stack is empty");
    else{
		printf("Popped element is %d\n", stack[top]);
		top--;
   }     
}

void display(){
int i;
if(top==-1){
    printf("\nStack is empty!!");
}
else{
    for(i=top; i>=0; --i){
    printf("%d\n",stack[i]);
        }
    }   
}  

int main()
{
    push(1);
    push(2);
    push(3);
    pop(3);
    display();
    return 0;
}


You can refer this video for understanding.


https://youtu.be/Mlv2fMvt9b4

https://youtu.be/1tDjNwntufU
Load comments