Stack

Stack is a common data structure for representing things that need to organized in a particular order. It follows the rule of last in first out (LIFO).

Following is the C++ code for stack:

#include<iostream>
#include<conio.h>
using namespace std;
class Stack
{
int arr[5];
int top;
public:
Stack()
{
top=-1;
}
void push(int v)
{
if(top==4)
cout<<“stack is full”<<endl;
else
{
arr[++top]=v;
cout<<“Data pushed successfully.”<<endl;
}
}

int pop()
{
{
if(top==-1)
{
cout<<“Stack empty.”<<endl;
return NULL;
}
else
return arr[top–];
}
}
};
void main()
{
Stack s;
s.push(10);
s.push(12);
s.push(13);
s.push(16);
s.push(18);
cout<<s.pop()<<endl;
cout<<s.pop()<<endl;
cout<<s.pop();
getch();
}

Print Friendly

Share this with your friends

One Comment to “Stack”

  1. Hats off to wheeovr wrote this up and posted it.

Leave a Reply

Your email address will not be published. Required fields are marked *