jeudi 17 septembre 2020

Not getting expected output...code is terminating unexpectedly (hackerank stack problem)

I am struggling with this code. program is terminating unexpectedly. please help problem link: https://www.hackerrank.com/challenges/maximum-element/problem

10
1 97
2
1 20
2
1 26
1 20
2
3
1 91

if we input the above numbers in our program...it doesn't get to the end but stops executing midway PLEASE HELP ME IN FINDING THE PROBLEM... is there a problem in dynamic allocation or any function...if you have time please visit problem link and help..thankyou

class stack
{
  public:
      int top = -1;
      int *s;
};

void create(stack *st,int n)
{
    st->s = new int[n];
}

void del(stack *st)
{
    st->top--;

}


void push(stack *st,int x)
{
    st->top++;
    st->s[st->top] = x;
    
}


int main()
{
    long int t;
    stack st;
    int choice;
    int number;
    int max =-1;
    
    cin>>t;
    
    create(&st,t);
    
    while(t--)
    {
        cin>>choice;
        
        if(choice == 1)
        {
            cin>>number;
            push(&st,number);
            if(max < number)
                max = number;
        }
        else if(choice == 2)
        {
            del(&st);
        }
        else
        {
           return max;
        }
        
    }
    
    

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire