-Using the C++ Standard Library
Implements a simple stack of integers. Can be modified to push user defined integers.
#include<iostream>
using namespace std;
#define MAX 10
class Stack
{
int val[MAX];
int top;
public:
Stack();
~Stack();
void push(int a);
void pop();
};
Stack::Stack()
{
top = -1;
}
Stack::~Stack()
{
cout<<"Desctructing"<<endl;
}
void Stack::push(int a)
{
if(top!=MAX)
{
top++;
val[top] = a;
cout<<val[top]<<" is pushed"<<endl;
cout<<endl;
}
else
{
cout<<"Stack is full"<<endl;
}
}
void Stack::pop()
{
if(top==-1)
{
cout<<"Stack is empty!"<<endl;
}
else
{
cout<<val[top]<<" is popped"<<endl;
cout<<endl;
val[top] = NULL;
top--;
}
}
int main()
{
Stack obj;
obj.push(10);
obj.push(20);
obj.push(30);
obj.pop();
obj.pop();
obj.pop();
cout<<endl;
cin.get();
return 0;
}

New Topic/Question
Reply



MultiQuote

|