Wednesday, 30 September 2026

Write a Program to reverse a string using stack


#include <iostream>

#include <stack>

#include <string>

using namespace std;


int main() {

    string input;

    stack<char> s;


    // Input the string to reverse

    cout << "Enter a string to reverse: ";

    cin>>input;

    // Push each character of the string onto the stack

    for (int i = 0; i < input.length(); i++) {

        s.push(input[i]); // Push the character at index i onto the stack

    }


    // Pop characters from the stack to get the reversed string

    string reversed;

    while (!s.empty()) {

        reversed += s.top();  // Get the top character from the stack

        s.pop();  // Remove the top character from the stack

    }


    // Display the reversed string

    cout << "Reversed string: " << reversed << endl;


    return 0;

}



Write a Program to Checking of balanced parenthesis using stack

 

#include <iostream>

#include <stack>//stack library,

using namespace std;


// Function to check if the parentheses in the expression are balanced

int Balanced(string expression) {

    stack<char> s;


    for (int i = 0; i < expression.length(); i++) {

        char ch = expression[i];


        switch (ch) {

            case '(':

            case '{':

            case '[':

                s.push(ch);  // Push opening brackets onto the stack

                break;

            case ')':

                if (s.empty() || s.top() != '(') return false;  // Check for matching opening bracket

                s.pop();

                break;

            case '}':

                if (s.empty() || s.top() != '{') return false;  // Check for matching opening bracket

                s.pop();

                break;

            case ']':

                if (s.empty() || s.top() != '[') return false;  // Check for matching opening bracket

                s.pop();

                break;

        }

    }


    // If the stack is empty, all brackets were matched

    return s.empty();

}


int main() {

    string expression;


    // Input the expression

    cout << "Enter an expression: ";

   cin>>expression; 

    // Check if the parentheses are balanced

    if (Balanced(expression)) {

        cout << "Parentheses are balanced." << endl;

    } else {

        cout << "Parentheses are not balanced." << endl;

    }


    return 0;

}


Write a program to implement a stack.

#include <iostream>

using namespace std;


#define MAX 100


int top = -1;

int stack[MAX];


// Push operation

void push(int value)

{

    if (top < MAX - 1)

    {

        top = top + 1;

        stack[top] = value;


        cout << value << " pushed to stack" << endl;

    }

    else

    {

        cout << "Stack Overflow! Cannot push " << value << endl;

    }

}


// Pop operation

int pop()

{

    if (top >= 0)

    {

        cout << stack[top] << " popped from stack" << endl;


        int value = stack[top];

        top = top - 1;


        return value;

    }

    else

    {

        cout << "Stack Underflow! Cannot pop" << endl;

        return -1;

    }

}


// Peek operation

int peek()

{

    if (top >= 0)

    {

        cout << "Top element is " << stack[top] << endl;

        return stack[top];

    }

    else

    {

        cout << "Stack is empty!" << endl;

        return -1;

    }

}


// Check whether stack is empty

int isEmpty()

{

    return (top == -1);

}


// Check whether stack is full

int isFull()

{

    return (top == MAX - 1);

}


// Main function

int main()

{

    push(10);

    push(20);

    push(30);


    peek();


    pop();


    peek();


    if (isEmpty())

        cout << "Stack is empty" << endl;

    else

        cout << "Stack is not empty" << endl;


    if (isFull())

        cout << "Stack is full" << endl;

    else

        cout << "Stack is not full" << endl;


    return 0;

}

Tuesday, 22 September 2026

to Check Whether a Matrix is Sparse or Not

 #include <iostream>

using namespace std;


int main()

{

    int matrix[3][3];

    int zero = 0;

    int total = 9;


    cout << "Enter 3 x 3 matrix:" << endl;


    // Input matrix

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            cin >> matrix[i][j];


            if (matrix[i][j] == 0)

            {

                zero++;

            }

        }

    }


    // Check sparse matrix

    if (zero > total / 2)

    {

        cout << "Matrix is a Sparse Matrix.";

    }

    else

    {

        cout << "Matrix is not a Sparse Matrix.";

    }


    return 0;

}




Write a Program to Addition of Two Sparse Matrices

 #include <iostream>

using namespace std;


int main()

{

    int matrix1[3][3];      int matrix2[3][3];


    int sparse1[10][3];      int sparse2[10][3];      int result[10][3];


    int k1 = 0;     int k2 = 0;      int k3 = 0;

    // Input Matrix 1

    cout << "Enter first 3 x 3 matrix:" << endl;

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            cin >> matrix1[i][j];

        }

    }

    // Input Matrix 2

    cout << "Enter second 3 x 3 matrix:" << endl;

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            cin >> matrix2[i][j];

        }

    }








    // Convert Matrix 1 into Sparse Matrix

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            if (matrix1[i][j] != 0)

            {

                sparse1[k1][0] = i;          // Row

                sparse1[k1][1] = j;          // Column

                sparse1[k1][2] = matrix1[i][j]; // Value

                k1++;

            }

        }

    }

    // Convert Matrix 2 into Sparse Matrix

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            if (matrix2[i][j] != 0)

            {

                sparse2[k2][0] = i;          // Row

                sparse2[k2][1] = j;          // Column

                sparse2[k2][2] = matrix2[i][j]; // Value

                k2++;

            }

        }

    }







    // Add two sparse matrices

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            int value = matrix1[i][j] + matrix2[i][j];

            if (value != 0)

            {

                result[k3][0] = i;       // Row

                result[k3][1] = j;       // Column

                result[k3][2] = value;   // Value

                k3++;

            }

        }

    }

    // Display Sparse Matrix 1

    cout << "\nSparse Matrix 1:" << endl;

    cout << "Row\tColumn\tValue" << endl;

    for (int i = 0; i < k1; i++)

    {

        cout << sparse1[i][0] << "\t"

             << sparse1[i][1] << "\t"

             << sparse1[i][2] << endl;

    }


    // Display Sparse Matrix 2

    cout << "\nSparse Matrix 2:" << endl;

    cout << "Row\tColumn\tValue" << endl;


    for (int i = 0; i < k2; i++)

    {

        cout << sparse2[i][0] << "\t"

             << sparse2[i][1] << "\t"

             << sparse2[i][2] << endl;

    }


    // Display Result

    cout << "\nAddition of Two Sparse Matrices:" << endl;

    cout << "Row\tColumn\tValue" << endl;

    for (int i = 0; i < k3; i++)

    {

        cout << result[i][0] << "\t"

             << result[i][1] << "\t"

             << result[i][2] << endl;

    }


    return 0;

}