Wednesday, 16 August 2017

//12-C Program-18 Stack implementation using Array:Insertion and Deletion


//12-D Program-18 Stack implementation using Array:Insertion and Deletion

#include<iostream.h>

#include<process.h>

int pop(int [], int &);

int Push(int [], int&, int);

void display(int [], int);

const int size=1;

int main()

{

  int Stack[size],Item,top=-1,res;

  char ch='y';

  while(ch=='y'|| ch=='Y')

  {

    cout<<"\n Enter ITEm for insertion :";

    cin>>Item;

    res=Push(Stack,top,Item);

    if(res==-1)

    {

      cout<<"Overflow !!! \n";

      exit(1);

    }

    cout<<"The stack now is:\n";

    display(Stack,top);

    cout<<"\n want to insert more elements ?(y/n)";

    cin>>ch;

  }

  cout<<"Now deletion of elements begins ...\n";

  ch='y';

  while(ch=='y' || ch=='Y')

  {

    res=pop(Stack,top);

    if(res==-1)

    {

      cout<<"Underflow \n";

      exit(1);

    }

    else

    {

      cout<<"\n Element deleted is:"<<endl;

      cout<<"\n The stack now is :\n";

      display(Stack,top);

    }

    cout<<"\n want to delete more elements ?\n";

   cin>>ch;

  }

   return 0;

 }

 int Push(int Stack[], int & top, int ele)

 {

    if(top == size-1) return -1;

    else

    {

       top++;

       Stack[top]=ele;

    }

    return 0;

 }

 int pop(int stack[], int &top)

 {

    int ret;

    if(top == -1) return -1;

    else

    {

      ret = stack[top];

      top--;

    }

    return ret;

 }

 void display(int stack[], int top)

 {

    if(top==-1) return;

    cout<<stack[top]<<"<-"<<endl;

    for(int i=top-1;i>=0;i--)

      cout<<stack[i]<<endl;

 }


No comments:

Post a Comment