What is the stack data structure?

A data structure is a specialized storage format that retrieves and processes data to utilize the main memory efficiently. Array, Stack, Queue, Linked List, Trees, etc., are famous data structures in computer science premises.

In addition, a stack is a data structure that works on the Last In First Out (LIFO) principle. The last item to be inserted into the stack is removed first.

For example, we have a pile of books. The topmost book should be removed from the stack whenever a book needs to be removed.

Generally, a stack is implemented using a list or an array. The stack implemented using an array will have a fixed size. Whereas a stack implemented using a linked list or ArrayList will be dynamic.

stack data structure
Stack Data Structure
#include<iostream>
using namespace std;
// node class
class Node {
public:
    int data;
    Node* next;
    ~Node()
    {
        next = nullptr;
    }
};
// stack class to handle stack
class Stack {
private:
    Node* top;
public:
    // stack class constructor
    Stack()
    {
        top = nullptr;
    }
    // push function
    bool push(const int val)
    {
        Node* temp = new Node;
        temp->data = val;
        temp->next = top;
        top = temp;
        return true;
    }
    // pop function
    int pop()
    { int val;
        if (!isEmpty())
        {
            Node* temp = top;
            val = top->data;
            top = top->next;
            delete temp;
            return val;
        }
        return 0;
    }
    // check stack is empty or not
    bool isEmpty()
    {
        if (top == nullptr)
            return true;
        return false;
    }
    // stack class destructor
    ~Stack()
    {
        while (top->next != nullptr)
        { 
            Node* temp = top;
            top = top->next;
            delete temp;
        }
    }
};

Explanation: Push(), Pop() Stack Data Structure Functions

push()

The push() function inserts a new element in the stack. It always inserts a new element at the top of the stack when pushed.

Primarily, it is implemented as a boolean function. It shall return true if the element is added successfully to the stack and false otherwise. However, the programmer can use a different return type according to the requirement.

Inside the parentheses, we pass the item as an argument when needed to push into the stack.

// main driver class to start program execution
int main()
{
    Stack S; // initiating stack class object or stack instance
    // push 1st value on stack
    if(S.push(5)){cout << "Pushed!" << endl;} else {cout << "Not pushed" << endl;}
    // push 2nd value on stack
    if(S.push(9)){cout << "Pushed!" << endl;} else {cout << "Not pushed" << endl;}
    // push 3rd value on stack
   if(S.push(8)){cout << "Pushed!" << endl;} else {cout << "Not pushed" << endl;}
    // pop top value of stack
    int val= S.pop(); //LIFO order read
    cout <<"Popped Value: " << val << endl;
    return 0;
}

pop()

The pop() function removes an element from the top of the stack. As the element removed is the most recently pushed element into the stack. Therefore, the element present at the top of the stack is removed.

Primarily, it is implemented as a boolean function. It returns true if the item is popped successfully from the stack and false otherwise. However, the programmer can use a different return type according to the requirement.

In the parentheses (function argument), we pass a variable by reference and return the item popped from our stack.

// main driver class to start program execution
int main()
{
    Stack S; // initiating stack class object or stack instance
    // push 1st value on stack
    if(S.push(6)){cout << "Pushed!" << endl;} else {cout << "Not pushed" << endl;}
    // push 2nd value on stack
    if(S.push(10)){cout << "Pushed!" << endl;} else {cout << "Not pushed" << endl;}
    // push 3rd value on stack
   if(S.push(15)){cout << "Pushed!" << endl;} else {cout << "Not pushed" << endl;}
    // pop top value of stack
    int val= S.pop(); //LIFO order read
    cout <<"Popped Value-1: " << val << endl;
    val= S.pop(); //LIFO order read
    cout <<"Popped Value-2: " << val << endl;
    return 0;
}

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...