STACK
A stack is a linear data structure in which an element can be inserted or deleted only at one end called the top of the stack. A stack follows the principle of Last-In-First-Out (LIFO) i.e. Last inserted element will be retrieved first .
According to the stack terminology, PUSH and POP are two terms used for insert and delete operations. This is a static implementation of stack through classes and objects.
PROGRAM
//Program to implement stack through classes and objects
#include <iostream>
#define SIZE 10
using namespace std;
class Stack
{
int stack[SIZE],top;
public:
Stack() //Constructor for initializing top
{
top=-1;
}
void push(int num)
{
if(top==SIZE-1) //Stack is full
{
cout<<"\nSTACK OVERFLOW";
}
else
{
top++;
stack[top]=num;
}
}
void pop()
{
int num;
if(top==-1) //Stack is empty
{
cout<<"\nSTACK UNDERFLOW";
}
else
{
num=stack[top];
top--;
cout<<"\nELEMENT DELETED: "<<num;
}
}
void traverse()
{
if(top==-1)
{
cout<<"\nSTACK IS EMPTY";
}
else
{
cout<<"\n\nSTACK ELEMENTS\n";
for(int i=top;i>=0;i--)
{
cout<<stack[i]<<endl;
}
}
}
};
int main()
{
Stack s;
s.traverse();
s.push(10);
s.push(20);
s.push(30);
s.traverse();
s.pop();
s.traverse();
return 0;
}
OUTPUT
A stack is a linear data structure in which an element can be inserted or deleted only at one end called the top of the stack. A stack follows the principle of Last-In-First-Out (LIFO) i.e. Last inserted element will be retrieved first .
According to the stack terminology, PUSH and POP are two terms used for insert and delete operations. This is a static implementation of stack through classes and objects.
PROGRAM
//Program to implement stack through classes and objects
#include <iostream>
#define SIZE 10
using namespace std;
class Stack
{
int stack[SIZE],top;
public:
Stack() //Constructor for initializing top
{
top=-1;
}
void push(int num)
{
if(top==SIZE-1) //Stack is full
{
cout<<"\nSTACK OVERFLOW";
}
else
{
top++;
stack[top]=num;
}
}
void pop()
{
int num;
if(top==-1) //Stack is empty
{
cout<<"\nSTACK UNDERFLOW";
}
else
{
num=stack[top];
top--;
cout<<"\nELEMENT DELETED: "<<num;
}
}
void traverse()
{
if(top==-1)
{
cout<<"\nSTACK IS EMPTY";
}
else
{
cout<<"\n\nSTACK ELEMENTS\n";
for(int i=top;i>=0;i--)
{
cout<<stack[i]<<endl;
}
}
}
};
int main()
{
Stack s;
s.traverse();
s.push(10);
s.push(20);
s.push(30);
s.traverse();
s.pop();
s.traverse();
return 0;
}
OUTPUT