Thursday, 15 October 2026

Write a Program to implement the queue and Display no.of elements.

 int front = 0; 

int rear = -1; 

int count = 0; 

  • front → points to the first element.

  • rear → points to the last element.

  • count → stores the number of elements in the queue.

For example:

Queue:  10   20   30

                 ↑         ↑

              front     rear


count = 3

When 10 is deleted:

Queue:       20   30

                    ↑    ↑

               front rear


count = 2

Enqueue = Insert
Dequeue = Delete
Count = Number of elements

#include <iostream>

using namespace std;


int queue[5];

int front = 0;

int rear = -1;

int count = 0;


// Insert element into Queue

void enqueue(int value)

{

    if (count == 5)

    {

        cout << "Queue is Full" << endl;

    }

    else

    {

        rear++;

        queue[rear] = value; 

        Count++;

        cout << value << " inserted" << endl;

    }

}


// Delete element from Queue

void dequeue()

{

    if (count == 0)

    {

        cout << "Queue is Empty" << endl;

    }

    else

    {

        cout << queue[front] << " deleted" << endl;

        front++;

        count--;

    }

}



// Peek/Front - View the first element

 void peek()

 { 

if (count == 0) 

{ 

cout << "Queue is Empty" << endl;

 } 

else

 { 

cout << "Front element: " << queue[front] << endl;

 }

} 


// Display Queue

void display()

{

    if (count == 0)

    {

        cout << "Queue is Empty" << endl;

    }

    else

    {

        cout << "Queue elements: ";


        for (int i = front; i <= rear; i++)

        {

            cout << queue[i] << " ";

        }


        cout << endl;

    }

}


// Display number of elements

void displayCount()

{

    cout << "Number of elements: " << count << endl;

}


int main()

{

    enqueue(10);

    enqueue(20);

    enqueue(30);


    display();


    displayCount();


    dequeue();

peek(); 

    display();


    displayCount();


    return 0;

}


Write a Program to implement a Dynamic queue.(Pointer ,Linked List)

//rear- Last

//front-first

#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

};


// Front and Rear pointers

Node* front = NULL;

Node* rear = NULL;



// Enqueue operation

void enqueue(int value) {


    // Create a new node

    Node* newNode = new Node;


    // Store the value

    newNode->data = value;

    newNode->next = NULL;


    // If queue is empty

    if (rear == NULL) {

        front = rear = newNode;

    }

    else {

        // Add new node at the rear

        rear->next = newNode;

        rear = newNode;

    }


    cout << value << " inserted into queue" << endl;

}



// Dequeue operation

void dequeue() {


    // Check if queue is empty

    if (front == NULL) {

        cout << "Queue is empty" << endl;

        return;

    }


    // Store the front node temporarily

    Node* temp = front;


    // Display the value being removed

    cout << temp->data << " deleted from queue" << endl;


    // Move front to the next node

    front = front->next;


    // If queue becomes empty

    if (front == NULL) {

        rear = NULL;

    }


    // Delete the old front node

    delete temp;

}



// Display queue

void display() {


    // Check if queue is empty

    if (front == NULL) {

        cout << "Queue is empty" << endl;

        return;

    }


    Node* temp = front;


    cout << "Queue: ";


    while (temp != NULL) {

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

        temp = temp->next;

    }


    cout << endl;

}



// Main function

int main() {


    // Insert elements

    enqueue(10);

    enqueue(20);

    enqueue(30);


    // Display queue

    display();


    // Delete one element

    dequeue();


    // Display queue again

    display();


    return 0;

}


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;

}