Sunday, 8 March 2015

Stack implementation using array


Implementation of stack using array have methods 

push(),pop(),reverse() and display.




/*
    Implementation of stack using array of size capacity
   
    Methods included are
    push();
    pop();
    display();
    reverse();
    @author: Vikas Bek
*/

#include<iostream>
#include <stdlib.h>
using namespace std;

void inputfunc();

template <class T>        /* Using Template for type independent */
class stack{
   
    public:
       
        T *arr;
        int capacity;
        int tos;
       
         stack(){
             tos=-1;
             capacity =100;
             arr=new T[capacity];
         }
       
        void push(stack &s,T value){
           
            if(s.tos==s.capacity){
                cout<<"stack is full"<<endl;
            }else{
                s.arr[++s.tos]=value;   
            }
           
        }
       
        bool isEmptyStack(stack a){
            if(a.tos==-1){
                return true;
            }else{
               
                return false;   
            }
        }
       
        stack reverse(stack &s){
            T data;
            if(isEmptyStack(s)){
                return s;
            }
            data=pop(s);
            reverse(s);
            insert_at_end(s, data);
            return s;
        }
       
        void insert_at_end(stack &s,T data){
           
            T temp;
            if(isEmptyStack(s)){
                push(s,data);
                return;
            }
           
            temp=pop(s);
            insert_at_end(s,data);
            push(s,temp);
           
        }


        T pop(stack &s){
           
            T top=s.arr[s.tos];
            if(s.tos==-1){
                cout<<"stack is empty"<<endl;
               
            }else{
                s.tos--;
            }
           
            return top;
        }

        T  display(stack s){
            if(s.tos<0){
                cout<<"stack is empty: \n";
                return -1;
            }else{
                cout<<"value in stack"<<endl;
                for(int i=s.tos;i>=0;i--)    {
                    cout<<"|| "<<s.arr[i]<<" ||"<<endl;
                }
                return s.arr[tos];
            }
        }

};


void inputfunc(){
    int ch=0;
    int value;
    stack <int>sa;
    stack <int>s1;
    do{
        cout<<"*******************MENU*******************"<<endl;
        cout<<"1. push "<<endl;
        cout<<"2. pop "<<endl;
        cout<<"3. display "<<endl;
        cout<<"4. reverse "<<endl; 
        cout<<"5. exit "<<endl;   
        cout<<"enter choice\n";
        cin>>ch;
        switch(ch){
            case 1: cout<<"Enter value to push in stack \n"<<endl;
                    cin>>value;
                    sa.push(s1,value);
                    sa.display(s1);
                    break;
            case 2: cout<<"Pop from stack \n"<<endl;
                    sa.pop(s1);
                    sa.display(s1);
                    break;
            case 3: cout<<"display: "<<endl;
                    sa.display(s1);
                    break;
            case 4: cout<<"reverse \n"<<endl;
                    s1=sa.reverse(s1);
                    sa.display(s1);
                    break;
            case 5: cout<<"exit!!!!!!! \n";
                    exit(0);
            default:  cout<<"wrong entery\n ";
           
        }
       
    }while(ch!=5);
   
   
}

int main(int argc, char** argv){
   
     inputfunc()    ;
   
    return 0;
}

 

No comments :

Post a Comment