adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ for stack using array

Program in C and C++ for stack using array

C PROGRAM
#include<stdio.h>
#include<conio.h>
#define size 25
  int top=-1;
  void push(int a[],int n)
    {
     if(top==9)
        printf("\nStact overflow");
     else
      {
        top++;
        a[top]=n;
   }
 }
   void pop(int a[])
    {
      if(top==-1)
    printf("\nStact underflow");
   else
    {
      printf("\nDeleted data is %d",a[top]);
      top--;
    }
 }
  void show(int a[])
       {
      int i;
       for(i=top;i>=0;i--)
     printf("%4d",a[i]);   
  }

  int main()
   {
     int a[size];
     
     push(a,23);
     push(a,34);
     push(a,56);
     push(a,89);
     push(a,12);
  
     printf("Elements of stack = ");
     show(a);
  
     pop(a);
  
     printf("\nElements of stack = ");
     show(a);
  
    getch();
   }
C++ PROGRAM
#include<iostream>
#include<conio.h>
 using namespace std;
 const int size=35;
  int top=-1;
  void push(int a[],int n)
    {
     if(top==9)
        cout<<"\nStact overflow";
     else
      {
        top++;
        a[top]=n;
   }
 }
   void pop(int a[])
    {
      if(top==-1)
    cout<<"\nStact underflow";
   else
    {
      cout<<"\nDeleted data is "<<a[top];
      top--;
    }
 }
  void show(int a[])
     {
      int i;
       for(i=top;i>=0;i--)
     cout<<"  "<<a[i];   
  }

  int main()
   {
     int a[size];
     
     push(a,23);
     push(a,34);
     push(a,56);
     push(a,89);
     push(a,12);
  
     cout<<"Elements of stack = ";
     show(a);
  
     pop(a);
  
     cout<<"\nElements of stack = ";
     show(a);
  
    getch();
   }
Output:-





Post a Comment

0 Comments