Header Ads

Seo Services

Stack Data Structure : Working of PUSH and POP Operation


Stack is a non primitive data structure which has one fixed end called as base and elements are added and removed to and from the stack with another end. The element top is used to point the location for adding and removing of elements. The Operation Push refers to adding new element to Stack and Operation Pop refers to removing the element from stack.



Algorithm of push

if(top==Max-1)
{
cout<<"\n the stack is full";
}
else
top++;
arr[top]=item;
////////////////////////////////////////

Algorithm of pop

if(top==-1)
{
cout<<"\n the stack is empty";
}
else
return arr[top];
top--;
}

Video Animation about Operations on Stack




Program



#include <stdio.h>
#include <conio.h>

#define MAX 10

struct stack
{
    int arr[MAX] ;
    int top ;
} ;

void initstack ( struct stack * ) ;
void push ( struct stack *, int item ) ;
int pop ( struct stack * ) ;

void main( )
{
    struct stack s ;
    int i ;

    clrscr( ) ;

    initstack ( &s ) ;

    push ( &s, 11 ) ;
    push ( &s, 23 ) ;
    push ( &s, -8 ) ;
    push ( &s, 16 ) ;
    push ( &s, 27 ) ;
    push ( &s, 14 ) ;
    push ( &s, 20 ) ;
    push ( &s, 39 ) ;
    push ( &s, 2 ) ;
    push ( &s, 15 ) ;
    push ( &s, 7 ) ;

    i = pop ( &s ) ;
    printf ( "\n\nItem popped: %d", i ) ;

    i = pop ( &s ) ;
    printf ( "\nItem popped: %d", i ) ;

    i = pop ( &s ) ;
    printf ( "\nItem popped: %d", i ) ;

    i = pop ( &s ) ;
    printf ( "\nItem popped: %d", i ) ;

    i = pop ( &s ) ;
    printf ( "\nItem popped: %d", i ) ;

    getch( ) ;
}

/* intializes the stack */
void initstack ( struct stack *s )
{
    s -> top = -1 ;
}

/* adds an element to the stack */
void push ( struct stack *s, int item )
{
    if ( s -> top == MAX - 1 )
    {
        printf ( "\nStack is full." ) ;
        return ;
    }
    s -> top++ ;
    s -> arr[s ->top] = item ;
}

/* removes an element from the stack */
int pop ( struct stack *s )
{
    int data ;
    if ( s -> top == -1 )
    {
        printf ( "\nStack is empty." ) ;
        return NULL ;
    }
    data = s -> arr[s -> top] ;
    s -> top-- ;
    return data ;
}

No comments:

Powered by Blogger.