Tuesday, 27 October 2026

Insertion at a Specific Position


Insert 15 at position 2 in the list 10 → 20 → 30.

The position is counted starting from 1. 

#include <iostream>

using namespace std;


struct Node

{

    int data;

    Node* next;

};


int main()

{

    Node* first = new Node;

    first->data = 10;


    Node* second = new Node;

    second->data = 20;


    Node* third = new Node;

    third->data = 30;


    first->next = second;

    second->next = third;

    third->next = NULL;


    Node* newNode = new Node;

    newNode->data = 15;


    Node* temp = first;


    temp = temp->next;


    newNode->next = temp->next;

    temp->next = newNode;


    temp = first;


    while (temp != NULL)

    {

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

        temp = temp->next;

    }


    return 0;

}


Insertion at the End


Logic: Reach the last node and connect it to the new node.


#include <iostream>

using namespace std;


struct Node

{

    int data;

    Node* next;

};


int main()

{

    Node* first = new Node;

    first->data = 10;

    first->next = NULL;

  • new Node creates a node in dynamic memory.

  • first->data = 10; stores 10 in the node.

  • first->next = NULL; indicates that there is no next node yet.

Linked list:

first → [10 | NULL]


    Node* newNode = new Node;

    newNode->data = 20;

    newNode->next = NULL;

  • new Node creates another node.

  • newNode->data = 20; stores 20 in the new node.

  • newNode->next = NULL; indicates that it is currently the last node.

Now, two separate nodes exist:

[10 | NULL] [20 | NULL]

They are not connected yet.


    Node* temp = first;

temp points to the first node containing 10.

temp → [10 | NULL]


    while (temp->next != NULL)

    {

        temp = temp->next;

    }

temp->next != NULL checks whether the current node has a next node.

temp = temp->next; moves temp to the next node.


    temp->next = newNode;

This statement connects the first node to the new node.

Before:

10 → NULL 20 → NULL

After:

10 → 20 → NULL

Now the insertion at the end is complete.


    temp = first;

We set temp back to the first node so that we can display the complete linked list. 

    while (temp != NULL)

    {

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

        temp = temp->next;

    }

This loop displays each node's data and moves to the next node.

Step

temp points to

Printed value

1

Node containing 10

10

2

Node containing 20

20

3

NULL

Loop stops

  return 0;

}


Insertion in Singly Linked List


Logic: Create a new node, connect it to the first node,

 and make it the new first node.



#include <iostream>

using namespace std;


struct Node

{

    int data;

    Node* next;

};


Every node contains two parts:

  • data stores the value.

  • next stores the address of the next node.



int main()

{

    Node* first = new Node;

    first->data = 20;

    first->next = NULL;


new Node creates a node in dynamic memory.

first->data = 20; stores 20 in the node.

first->next = NULL; indicates that no next node exists yet.


    Node* newNode = new Node;

    newNode->data = 10;

  • new Node creates another node.

  • newNode->data = 10; stores 10 in the new node.

Now there are two nodes, but they are not connected yet.


    newNode->next = first; This statement makes the new node containing 10 point to the existing node containing 20. 

  

  first = newNode; Now first points to the node containing 10. Therefore, 10 becomes the first node.  

 The final linked list is:

10 → 20 → NULL



    Node* temp = first; temp is used to visit the nodes one by one.

Initially, temp points to the first node containing 10.



    while (temp != NULL)

    {

        cout << temp->data << " "; displays the current node's data. 

        temp = temp->next; moves to the next node. 

    }

Step 1-Displays 10, moves to 20.

2-Displays 20,moves to NULL.

4-Stops because temp != NULL is false.


    return 0;

}


C++ program to create and display a singly linked list


#include <iostream>

using namespace std;


struct Node

{

    int data;

    Node* next;

};


Every node has two parts: data and next.

  • int data-stores an integer such as 10.

  • Node* next-stores the address of the next node.

  • struct Node- is the design or blueprint for creating nodes.



int main()

{

    Node* first = new Node;

    Node* second = new Node;

    Node* third = new Node;


These statements create three nodes dynamically in memory.

  • new Node -allocates memory for one node.

  • first- points to the first node.

  • second- points to the second node.

  • third- points to the third node.


    first->data = 10;

    first->next = second;

  • first->data = 10; stores 10 in the first node.

  • first->next = second; connects the first node to the second node.

Here, -> is used to access a member of a structure through a pointer.



    second->data = 20;

    second->next = third;


    third->data = 30;

    third->next = NULL;

  • third->data = 30; stores 30 in the third node.

  • third->next = NULL; indicates that no node comes after the third node.

NULL marks the end of the linked list.


    Node* temp = first;


temp is a pointer used to visit each node.

Initially, temp points to the first node containing 10.

We use temp to display the list without changing first.


    while (temp != NULL)  Checks whether a node exists 

    {

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

        temp = temp->next;

    }


This loop visits each node one by one.

temp → 10First iteration

Prints 10; moves to the second node.

temp → 20 Second iteration

Prints 20; moves to the third node.

temp → 30 Third iteration

Prints 30; moves to NULL.

temp → NULL

The condition becomes false, so the loop stops.


    delete first;

    delete second;

    delete third;


    return 0;

}


Tuesday, 20 October 2026

Priority Queue

 
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
    // Max Priority Queue
    priority_queue<int> maxPQ;
    maxPQ.push(30);
    maxPQ.push(10);
    maxPQ.push(50);
    maxPQ.push(20);
    maxPQ.push(40);
   

 cout << "Max Priority Queue: ";
    while (!maxPQ.empty())
    {
        cout << maxPQ.top() << " ";
        maxPQ.pop();
    }
    cout << endl;
    // Min Priority Queue
    priority_queue<int, vector<int>, greater<int>> minPQ;
    minPQ.push(30);
    minPQ.push(10);
    minPQ.push(50);
    minPQ.push(20);
    minPQ.push(40);
    cout << "Min Priority Queue: ";
    while (!minPQ.empty())
    {
        cout << minPQ.top() << " ";
        minPQ.pop();
    }
    cout << endl;
    return 0;
}