Friday 20 September 2013

Friday 20 September 2013

C program for Stack


C program for performing Stack Operation


#include <stdio.h>
#include <stdlib.h>

void push(int);
void pop();
void display();

static int a[10];
static  int top=-1;
int main(){

push(1);
push(5);
push(8);
pop();
push(12);
push(11);

display();
return 0;
  
}
    void push(int i){
        if(top==9){
        printf("stack overflow");
        }
        else
        a[++top]=i;
        
    }
  
    void pop(){
        if(top== -1){
        printf("stack underflow");
        }
        else
        top--;
    }
  
    void display(){
        int i;
        for(i=0;i<top+1;i++)
        printf("%d ",a[i]);
    }


Executing the program....
$demo
1 5 12 11 

No comments:

Post a Comment