Monday, August 13, 2018

Program On Dynamic Stack

"Nice One Program"

#include<iostream.h>
#include<conio.h>

int top=-1,size;
void push(int *p,int val)
{
if(top==-1)
{top=0;
p[top++]=val;
}
else if(top==size)
cout<<"\nOverflow:\n";
else
p[top++]=val;
}
//pop func

void pop()
{
       if(top==-1)
       cout<<"\nUnderflow:\n";
       else
       top--;

}

void main()
{  
     clrscr();
    
    cout<<"Enter size of array:";
    cin>>size;
    int *p=new int[size];
    int ch,item;
    cout<<"1 PUSH:";
    cout<<"2 POP:";
    cout<<"3 Exit:";
    cin>>ch;
   
  while(ch!=3)

{
if(ch==1)
{    
                 cout<<"Enter Ele:";

         cin>>item;
         push(p,item);

}

else if(ch==2)

             pop();
}

else if(ch==3)
break;

cout<<"1 PUSH:";
cout<<"2 POP:";
cout<<"3 Exit:";
cin>>ch;

}

for(int i=0;i<size;i++)
        cout<<p[i]<<" ";

getch();
}

No comments:

Post a Comment