C++ - STACK Implementation using C++ Class with PUSH, POP, TRAVERSE Operations

In this code snippet we will learn how to implement STACK using Class in C++ programming language?

In this example we will implement stack with the help of c++ class and object, in this example (code snippet) stack will be implemented with following operations

  1. Stack Initialization
  2. Push Operation
  3. Pop Operations
  4. Check Empty
  5. Check Full
  6. Stack Traversing to Display Stack Items

STACK Implementation using C++ Class with PUSH, POP, TRAVERSE Operations

#include <iostream>

#define SIZE 5

using namespace std;

class STACK {
private:
    int num[SIZE];
    int top;

public:
    STACK(); //defualt constructor
    int push(int);
    int pop();
    int isEmpty();
    int isFull();
    void displayItems();
};

STACK::STACK()
{
    top = -1;
}

int STACK::isEmpty()
{
    if (top == -1)
        return 1;
    else
        return 0;
}

int STACK::isFull()
{
    if (top == (SIZE - 1))
        return 1;
    else
        return 0;
}

int STACK::push(int n)
{
    //check stack is full or not
    if (isFull()) {
        return 0;
    }
    ++top;
    num[top] = n;
    return n;
}

int STACK::pop()
{
    //to store and print which number
    //is deleted
    int temp;
    //check for empty
    if (isEmpty())
        return 0;
    temp = num[top];
    --top;
    return temp;
}

void STACK::displayItems()
{
    int i; //for loop
    cout << "STACK is: ";
    for (i = (top); i >= 0; i--)
        cout << num[i] << " ";
    cout << endl;
}

// Main code
int main()
{
    //declare object
    STACK stk;
    int choice, n, temp;

    do {
        cout << endl;
        cout << "0 - Exit." << endl;
        cout << "1 - Push Item." << endl;
        cout << "2 - Pop Item." << endl;
        cout << "3 - Display Items (Print STACK)." << endl;

        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) {
        case 0:
            break;

        case 1:
            cout << "Enter item to insert: ";
            cin >> n;
            temp = stk.push(n);
            if (temp == 0)
                cout << "STACK is FULL." << endl;
            else
                cout << temp << " inserted." << endl;
            break;

        case 2:
            temp = stk.pop();
            if (temp == 0)
                cout << "STACK IS EMPTY." << endl;
            else
                cout << temp << " is removed (popped)." << endl;
            break;

        case 3:
            stk.displayItems();
            break;

        default:
            cout << "An Invalid choice." << endl;
        }
    } while (choice != 0);

    return 0;
}

Output

    0 - Exit.
    1 - Push Item.
    2 - Pop Item.
    3 - Display Items (Print STACK).
    Enter your choice: 1 
    Enter item to insert: 10  
    10 inserted.
    
    0 - Exit.
    1 - Push Item.  
    2 - Pop Item.   
    3 - Display Items (Print STACK).    
    Enter your choice: 1 
    Enter item to insert: 20  
    20 inserted.    
    
    0 - Exit.
    1 - Push Item.  
    2 - Pop Item.   
    3 - Display Items (Print STACK).  
    0 - Exit.
    1 - Push Item.  
    2 - Pop Item.   
    3 - Display Items (Print STACK).    
    Enter your choice: 3 
    STACK is: 20 10 
    
    0 - Exit.
    1 - Push Item.  
    2 - Pop Item.   
    3 - Display Items (Print STACK).    
    Enter your choice: 2 
    20 is removed (popped).   
    
    0 - Exit.
    1 - Push Item.  
    2 - Pop Item.   
    3 - Display Items (Print STACK).    
    Enter your choice: 3 
    STACK is: 10   

   
    0 - Exit.
    1 - Push Item.  
    2 - Pop Item.   
    3 - Display Items (Print STACK).    
    Enter your choice: 0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.