Stack Pop Operation

Algorithm

INPUT: TOP POINTER WHICH IS USED TO POINT TO THE TOP OF THE STACK
OUTPUT: STACK AFTER POP OPERATION
DATA STRUCTURE: STACK USING ARRAY

STEP 1: START
STEP 2: IF(TOP=-1)THEN
STEP 3: PRINT("STACK UNDERFLOWS")
STEP 4: ELSE
STEP 5: ITEM=A[TOP]
STEP 6: TOP=TOP-1
STEP 7: ENDIF
STEP 8: 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();
struct node *ptr,*news,*header;
void display();void push();void pop();
int main()
{ 
    top++;
    a[top]=5;
    
    pop();
    display();
    return 0;
}

void pop()
{
 if(top==-1)
 {
 printf("stack underflow");
 }
 else{
 item=a[top];
 top--;
 }
}
void display()
{
 if(top==-1)
 {
 printf("stack underflows");
 }
 for(int i=top;i>=0;--i)
 {
 printf("%d ",a[i]);
 }
 printf("\n");
}

Output

user@computer$ : stack underflows

Leave a Comment

Your email address will not be published. Required fields are marked *