Algorithm
INPUT: TOP POINTER WHICH IS USED TO POINT TO THE TOP OF THE STACK AND THE ELEMENT TO BE PUSH
OUTPUT: STACK AFTER PUSH OPERATION
DATA STRUCTURE: STACK USING ARRAY
STEP 1: START
STEP 2:IF(TOP=SIZE-1)THEN
STEP 3: PRINT("STACK OVERFLOWS")
STEP 4: ELSE
STEP 5:TOP=TOP+1
STEP 6: A[TOP]=ITEM
STEP 7: END
Code
#include <stdio.h>
#include <stdlib.h>
int size;
int top=-1,item,size=4;
int a[4],n;
struct node {
int data;
struct node *link;
};
void newnode();
void display();void push();
int main()
{
push();
printf("Stack after Push Operation");
display();
return 0;
}
void push(){
if(top==size-1)
{printf("Stack Overflow");}
else {
printf("Enter the item you want to insert?\n");
scanf("%d",&item);
top=top+1;
a[top]=item;
}
}
void display()
{
if(top==-1)
{
printf("stack underflows");
}
for(int i=top;i>=0;--i)
{
printf("%d ",a[i]);
}
printf("\n");
}
Output
user@computer$ : Enter the item you want to insert?
5
Stack after Push Operation5