Tuesday, 6 October 2026

C++ program to implement a Dynamic Stack using Pointer and Linked List.

 

#include <iostream>

using namespace std;

 

// Structure for a Node

struct Node {

    int data;       // Stores the value

    Node* next;     // Stores the address of the next node

};

 

// Top pointer of the stack

Node* top = NULL;

 

// Push operation

void push(int value) {

   

    // Create a new node

    Node* newNode = new Node;

 

    // Store the value

    newNode->data = value;

 

    // Connect new node to the current top

    newNode->next = top;

 

    // Make new node the new top

    top = newNode;

 

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

}

 

// Pop operation

void pop() {

   

    // Check if stack is empty

    if (top == NULL) {

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

        return;

    }

 

    // Store the top node temporarily

    Node* temp = top;

 

    // Display the value being removed

    cout << temp->data << " popped from stack" << endl;

 

    // Move top to the next node

    top = top->next;

 

    // Delete the old top node

    delete temp;

}

 

 

// Display stack

void display() {

   

    // Check if stack is empty

    if (top == NULL) {

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

        return;

    }

 

    // Start from the top

    Node* temp = top;

 

    cout << "Stack: ";

 

    // Display all nodes

    while (temp != NULL) {

        cout << temp->data << " ";

        temp = temp->next;

    }

 

    cout << endl;

}

 

int main() {

 

    // Push elements

    push(10);

    push(20);

    push(30);

 

    display();

 

    pop();

 

    display();

 

    return 0;

}

Monday, 5 October 2026

Evaluation of Prefix Expression

 #include <iostream>

#include <stack>

#include <cctype>


using namespace std;


int main() {


    string exp;

    stack<int> s;


    cout << "Enter prefix expression: ";

    cin >> exp;


    // Read from right to left

    for (int i = exp.length() - 1; i >= 0; i--) {


        char ch = exp[i];


        if (isdigit(ch)) {

            s.push(ch - '0');

        }

        else {

            int a = s.top();

            s.pop();


            int b = s.top();

            s.pop();


            if (ch == '+')

                s.push(a + b);


            else if (ch == '-')

                s.push(a - b);


            else if (ch == '*')

                s.push(a * b);


            else if (ch == '/')

                s.push(a / b);

        }

    }


    cout << "Result = " << s.top();


    return 0;

}


Evaluation of Postfix Expression

#include <iostream>

#include <stack>

#include <cctype>


using namespace std;


int main() {

    string exp;

    stack<int> s;


    cout << "Enter postfix expression: ";

    cin >> exp;


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

        char ch = exp[i];


        // If character is a number

        if (isdigit(ch)) {

            s.push(ch - '0');

        }

        else {

            // Get second operand

            int b = s.top();

            s.pop();


            // Get first operand

            int a = s.top();

            s.pop();


            // Perform operation

            if (ch == '+') {

                s.push(a + b);

            }

            else if (ch == '-') {

                s.push(a - b);

            }

            else if (ch == '*') {

                s.push(a * b);

            }

            else if (ch == '/') {

                s.push(a / b);

            }

        }

    }


    cout << "Result = " << s.top();


    return 0;

}


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;

}