STACK Implementation using Array with PUSH, POP, TRAVERSE Operations

Views: 11519
Comments: 0
Like/Unlike: 2
Posted On: 30-Dec-2017 09:50 

Share:   fb twitter linkedin
26 Points
11 Posts


Stack is a simple data structure with a predefined capacity that allows adding and removing elements in a particular order i.e. Stack work on LIFO (Last in, First out) technique. Every time an element is added, it goes on the top of the stack, the only element that can be removed is the element that was at the top of the stack. The Stack falls in linear data structure. The stack was first proposed in 1946.


In this example we will implement stack with the help of class and object, in this example stack will be implemented with following operations:

  • Stack Initialization
  • Push Operation
  • Pop Operations
  • Check Empty
  • Check Full
  • Stack Traversing to Display Stack Items
#define SIZE 5
namespace std {}
using namespace std;
class STACK
        int num[SIZE];
        int top;
        int push(int);
        int pop();
        int isEmpty();
        int isFull();
        void displayItems();
int STACK::isEmpty()
        return 1;
        return 0;
int STACK::isFull()
        return 1;
        return 0;
int STACK::push(int n)
        return 0;
    num [top]=n;
    return n;
int STACK::pop()
    int temp;
        return 0;
    return temp;
void STACK::displayItems(){
    int i;
    cout<<"STACK is:";
int main()
    STACK stk;
    int choice,n,temp;
    do {
        cout<<"1-Push Item."<<endl;
        cout<<"2-Pop Item."<<endl;
        cout<<"3 - Display Items (Print STACK)."<<endl;

        cout<<"Enter your choice: ";

        case 0: break;

        case 1:
            cout<<"Enter item to insert: ";
                cout<<"STACK is FULL."<<endl;
                cout<<temp<<" inserted."<<endl;

        case 2:
                cout<<"STACK IS EMPTY."<<endl;
                cout<<temp<<" is removed (popped)."<<endl;

        case 3:

            cout<<"An Invalid choice."<<endl;

    return 0;



Hope it will help to understand the stack data strucure in C++.

 Log In to Chat