Create Stack Using Array In C

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

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

Create Stack Using Array In C

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

#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

Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment