Stack Program containing Top Of Stack (TOS) and LIFO Features


#include <iostream.h>
#include <conio.h>

#define SIZE 100
// This creates the class stack.
class stack 
{
    int stck[SIZE];
    int tos;
    public:
    void init();
    void push(int i);
    int pop();
};

void stack::init()
{
    tos = 0;
}

void stack::push(int i)
{
    if(tos==SIZE) 
    {
        cout << "Stack is full.\n";
        return;
    }
stck[tos] = i;
tos++;
}

int stack::pop()
{
    if(tos==0) 
    {
        cout << "Stack underflow.\n";
        return 0;
    }
    tos--;
    return stck[tos];
}

int main()
{
    clrscr();
    stack stack1, stack2; // create two stack objects
    stack1.init();
    stack2.init();
    stack1.push(1);
    stack2.push(2);
    stack1.push(3);
    stack2.push(4);
    cout << stack1.pop() << " ";
    cout << stack1.pop() << " ";
    cout << stack2.pop() << " ";
    cout << stack2.pop() << "\n";
    getch();
    return 0;
}

Custom Search