Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 months ago
1.6 kB
2
Indexable
Never
#include <iostream>

class Stack
{
public:
    Stack()
    {
        size = 20;
        items = new char* [ size ];
        top_index = -1;
    }

    ~Stack()
    {
        delete[] items;
    }

    void push( char values[] )
    {
        if( !full() )
        {
            ++top_index;
            items[ top_index ] = values;
        }
        else
        {
            std::cout << "Stack is full!" << std::endl;
            return;
        }
    }

    char* pop()
    {
        if( !empty() )
        {
            char* popped = items[ top_index ];
            --top_index;
            return popped;
        }
        else
        {
            std::cout << "Stack is empty!" << std::endl;
            return nullptr;
        }
    }

    char* top()
    {
        if( !empty() )
        {
            return items[ top_index ];
        }
        else
        {
            std::cout << "Stack is empty!" << std::endl;
            return nullptr;
        }
    }

    bool empty() const
    {
        return top_index == -1;
    }

    bool full() const
    {
        return top_index == size - 1;
    }

private:
    char** items;
    int top_index;
    int size;
};

int main()
{
    //char expression[] = "12 + 2 * ( 3 * 4 + 10 / 5 ).";

    //std::cout << expression << std::endl;

    Stack s1;
    s1.push( "12 + 2");
    s1.push( "/");

    std::cout << s1.top() << std::endl;

    s1.pop();
    std::cout << s1.top() << std::endl;

    return 0;
}
Leave a Comment