Skip to content

Instantly share code, notes, and snippets.

@mycodeschool
Created October 8, 2013 02:44
Show Gist options
  • Save mycodeschool/6878628 to your computer and use it in GitHub Desktop.
Save mycodeschool/6878628 to your computer and use it in GitHub Desktop.
An object oriented implementation of stack using arrays in C++.
// Stack - Object oriented implementation using arrays
#include <iostream>
using namespace std;
#define MAX_SIZE 101
class Stack
{
private:
int A[MAX_SIZE]; // array to store the stack
int top; // variable to mark the top index of stack.
public:
// constructor
Stack()
{
top = -1; // for empty array, set top = -1
}
// Push operation to insert an element on top of stack.
void Push(int x)
{
if(top == MAX_SIZE -1) { // overflow case.
printf("Error: stack overflow\n");
return;
}
A[++top] = x;
}
// Pop operation to remove an element from top of stack.
void Pop()
{
if(top == -1) { // If stack is empty, pop should throw error.
printf("Error: No element to pop\n");
return;
}
top--;
}
// Top operation to return element at top of stack.
int Top()
{
return A[top];
}
// This function will return 1 (true) if stack is empty, 0 (false) otherwise
int IsEmpty()
{
if(top == -1) return 1;
return 0;
}
// ONLY FOR TESTING - NOT A VALID OPERATION WITH STACK
// This function is just to test the implementation of stack.
// This will print all the elements in the stack at any stage.
void Print() {
int i;
printf("Stack: ");
for(i = 0;i<=top;i++)
printf("%d ",A[i]);
printf("\n");
}
};
int main()
{
// Code to test the implementation.
// calling Print() after each push or pop to see the state of stack.
Stack S;
S.Push(2);S.Print();
S.Push(5);S.Print();
S.Push(10);S.Print();
S.Pop();S.Print();
S.Push(12);S.Print();
}
@DivyanshRathi
Copy link

I am sorry but I have a little doubt,....we defined a function in public as Stack which sets the value of top as -1. But we never called it in the main function, then why the stack is initialized with top = -1.

@Ahbar1999
Copy link

@DivyanshRathi a constructor is called automatically when a class' object is instantiated

@csgeeek
Copy link

csgeeek commented May 25, 2021

stdio needs to be included.

No, It isn't necessary.
It will work fine even if we don't put #include<stdio.h> in C/C++.

@csgeeek
Copy link

csgeeek commented May 25, 2021

I am sorry but I have a little doubt,....we defined a function in public as Stack which sets the value of top as -1. But we never called it in the main function, then why the stack is initialized with top = -1.

Stack is a constructor.
It will be called automatically when an object is created in int main()

@itsakashp
Copy link

itsakashp commented Jun 14, 2021

But if i am not wrong, the basic functionality of pop() function in stack is to delete the recent value and also return the deleted Value to the Calling Function
.int pop(){ if (isEmpty()) { cout<<"ERROR! No Element to POP\t"; } return A[top--]; };

@Abhinav12082003
Copy link

stdio needs to be included.

actually iostream is enough in cpp language

@JoNieRv
Copy link

JoNieRv commented Jan 2, 2023

stdio needs to be included.

actually iostream is enough in cpp language

he said that because printf is used in code instead of cout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment