Sunday, 18 October 2026

Circular queue

 


#include <iostream>

using namespace std;


int main()

{

    int q[5], front = -1, rear = -1;

    int choice, value, i;


    do

    {

        cout << "\n1. Insert";

        cout << "\n2. Delete";

        cout << "\n3. Display";

        cout << "\n4. Exit";

        cout << "\nEnter choice: ";

        cin >> choice;


        switch (choice)

        {

            case 1:

                if ((rear + 1) % 5 == front)

                    cout << "Queue is Full";


 For example, if rear = 4 and front = 0:

[ (4+1)%5=0 ]

Since 0 == front, the queue is full.


                else

                {

                    cout << "Enter value: ";

                    cin >> value;


                    if (front == -1)

                        front = 0;

If the queue is empty, front is set to 0 because the first element is being inserted. 

                    rear = (rear + 1) % 5;

                    q[rear] = value;

                }



     rear = (rear + 1) % 5; moves rear to the next position.

q[rear] = value; stores the value at that position.

Example: If rear = -1, then:[ (-1+1)%5=0 ]

Therefore, rear = 0, and the value is stored at q[0].


                break;


            case 2:

                if (front == -1)

                    cout << "Queue is Empty";

                else

                {

                    cout << "Deleted: " << q[front];


                    if (front == rear)

                        front = rear = -1;

If front == rear, there is only one element in the queue.

After deleting it, both front and rear are set to -1, indicating that the queue is empty.


                    else

                        front = (front + 1) % 5;

This moves front to the next position.

For example, if front = 2:

[ (2+1)%5=3 ]

Now, front = 3.


                }

                break;


            case 3:

                if (front == -1)

                    cout << "Queue is Empty";

                else

                {

                    i = front;

The variable i starts at the front position because we want to display elements from the beginning of the queue. 


                    while (true)

                    {

                        cout << q[i] << " ";


                        if (i == rear)

When i reaches rear, the last element has been displayed. break stops the loop. 

                            break;


                        i = (i + 1) % 5;


                    }

                }

                break;

        }


    } while (choice != 4);


    return 0;

}





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;

}