w3resource

C++ Stack Exercises: Check the size and empty status of a stack (linked list)

C++ Stack: Exercise-17 with Solution

Write a C++ program to check a stack's size and whether it is empty or not. The stack is implemented using a linked list.

Test Data:
Check a stack (using linked list) is empty or not!
Stack is empty!
Input some elements onto the stack:
Stack elements are: 0 1 3 5 6
Size of the stack is 5

Sample Solution:

C++ Code:

#include <iostream>

using namespace std;

// Define the node structure for the linked list
struct Node {
    int data;
    Node* next;
};

class Stack {
private:
	// Top-of-stack pointer
    Node* top;
    // This variable keeps track of the stack size
	int size;  

public:
    // Constructor to initialize an empty stack
    Stack() {
        top = NULL; // Initialize top pointer to NULL for an empty stack
        size = 0; // Initialize the size of the stack to zero
    }

    // Function to push an element onto the stack
    void push(int x) {
        Node* new_Node = new Node(); // Create a new node
        new_Node->data = x; // Assign data to the new node
        new_Node->next = top; // Point the new node's next to the current top node
        top = new_Node; // Update top to the new node
        size++; // Increment the size of the stack
    }

    // Function to pop an element from the stack
    void pop() {
        if (top == NULL) {
            cout << "Stack is empty!" << endl; // Display message if the stack is empty
            return;
        }
        Node* temp = top; // Store the current top node
        top = top->next; // Move top to the next node
        size--; // Decrement the size of the stack
        delete temp; // Delete the previous top node
    }

    // Function to display the elements of the stack
    void display() {
        if (top == NULL) {
            cout << "Stack is empty!" << endl; // Display message if the stack is empty
            return;
        }
        Node* temp = top; // Create a temporary node to traverse the stack
        cout << "Stack elements are: ";
        while (temp != NULL) {
            cout << temp->data << " "; // Display the data of each node
            temp = temp->next; // Move to the next node
        }
        cout << endl;
    }

    // Function to check if the stack is empty
    bool isEmpty() {
        return (top == NULL); // Return true if the top pointer is NULL, else false
    }

    // Function to find the size of the stack
    int getSize() {
        return size; // Return the size of the stack
    }
};

int main() {
    Stack stk; // Create an object of Stack class
    cout << "Check a stack (using linked list) is empty or not!\n";
    stk.display(); // Display the stack status
    cout << "\nInput some elements onto the stack:\n";
    stk.push(6);
    stk.push(5);
    stk.push(3);
    stk.push(1);
    stk.push(0);
    stk.display(); // Display the elements in the stack
    cout << "\nSize of the stack is " << stk.getSize() << endl; // Display the size of the stack
    cout << "\nRemove two elements from the said stack:\n";
    stk.pop();
    stk.pop();
    stk.display(); // Display the updated stack
    cout << "Size of the stack is " << stk.getSize() << endl; // Display the size of the stack
    return 0;
}

Sample Output:

Check a stack (using linked list) is empty or not!
Stack is empty!

Input some elements onto the stack:
Stack elements are: 0 1 3 5 6 

Size of the stack is 5

Remove two elements form the said stack:
Stack elements are: 3 5 6 
Size of the stack is 3

Flowchart:

Flowchart: Check the size and empty status of a stack (linked list).
Flowchart: Check the size and empty status of a stack (linked list).
Flowchart: Check the size and empty status of a stack (linked list).

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Implement a stack using a linked list with push, pop.
Next C++ Exercise: Reverse the elements of a stack (using a linked list).

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.