Q1 B

 avatar
Sameh
c_cpp
2 months ago
1.3 kB
2
Indexable
Never
class arrayStack
{
    int * stackArray , sizeOfStack , top;
public:
    arrayStack(int sizeOfArray = 1)
    {
        sizeOfStack = sizeOfArray;
        stackArray = new int [sizeOfStack];
        top = 0;
    }
    bool isFull()
    {
        return sizeOfStack == top;
    }
    void doubleSize()
    {
        int * tempArray = new int [sizeOfStack * 2];
        for(int i = 0 ; i < sizeOfStack ; i++)
        {
            tempArray[i] = stackArray[i];
        }
        sizeOfStack *= 2;
        delete[] stackArray;
        stackArray = tempArray;
    }
    void push(int data)
    {
        if(isFull())
            doubleSize();
        stackArray[top] = data;
        top++;
    }
    bool isEmpty()
    {
        return top == 0;
    }
    int pop()
    {
        if(isEmpty())
        {
            cout << "Stack is empty\n";
            return -1;
        }
        top--;
        return stackArray[top];
    }
    int getNumberOfElements()
    {
        return top;
    }
    void printStack()
    {
        for(int i = top - 1 ; i>= 0; i--)
            cout << stackArray[i] << "\t";
    }
    void deleteStack()
    {
        top = 0;
    }
    ~arrayStack()
    {
        cout << "Stack is deleted\n";
        delete[] stackArray;
    }
};
Leave a Comment