Skip to content

Instantly share code, notes, and snippets.

@harish-r
Last active August 29, 2015 14:06
Show Gist options
  • Save harish-r/35d0739a8652b26185ee to your computer and use it in GitHub Desktop.
Save harish-r/35d0739a8652b26185ee to your computer and use it in GitHub Desktop.
Stack - Array implementation in C++
/* Stack - Array Implementation */
/* Language: C++ */
/* Created By: Harish R */
#include<iostream>
#define MAX 10
using namespace std;
class Stack
{
int tos;
int stack_array[MAX];
bool isEmpty()
{
return tos == -1;
}
bool isFull()
{
return tos == MAX-1;
}
public:
Stack()
{
tos = -1;
}
void push(int x)
{
if(!isFull())
{
stack_array[++tos] = x;
}
else
cout << "Stack is Full!" << endl;
}
void pop()
{
if(!isEmpty())
{
tos--;
}
else
cout << "Stack is Empty!" << endl;
}
void find(int x)
{
int flag = 0;
if(!isEmpty())
{
for(int i=0;i<=tos;i++)
{
if(stack_array[i] == x)
{
cout << "Found at position: " << i << endl;
flag = 1;
break;
}
}
if(flag == 0)
cout << "Number not found!" << endl;
}
else
cout << "Stack is Empty!" << endl;
}
void display()
{
if(!isEmpty())
{
for(int i=0;i<=tos;i++)
cout << stack_array[i] << " ";
cout << endl;
}
else
cout << "Stack is Empty!" << endl;
}
};
int main()
{
Stack s;
s.push(15);
s.push(25);
s.push(35);
s.push(45);
s.push(55);
s.display();
s.find(35);
s.find(65);
s.pop();
s.display();
s.pop();
s.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment