> For the complete documentation index, see [llms.txt](https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/29-linked-list-i/code.md).

# Code

```cpp
#include <bits/stdc++.h>
using namespace std;

class Node
{
    public:
    int data;
    Node *next;
    
    Node (int d) {
        this->data = d;
        this->next = NULL;
    }
};

void traverse(Node *&head) {
    Node *temp = head;
    while(temp != NULL) {
        cout << temp -> data << ' ';
        temp = temp -> next;
    }
    cout << endl;
}

void insertAtHead(Node* &head, int d) {
    // step 1: Create new node
    Node *newNode = new Node(d);
    // step 2: set next of new node to head
    newNode -> next = head;
    // step 3: update head
    head = newNode;
}

void insertAtTail(Node* &tail, int d) {
    // step 1: Create new node
    Node *newNode = new Node(d);
    // step 2: set next of tail to new Node
    tail -> next = newNode;
    // step 3: update tail
    tail = newNode;
}

void insertAtPos(Node* &head, int pos, int d) {
    // boundary checking
    // udpate tail
    if(pos == 1) 
        insertAtHead(head, d);
    else {
        // ste 1: create new Node
        Node *newNode = new Node(d);
        // step 2: traverse
        Node * prev = head;
        int t = pos - 2;
        while(t--) 
            prev = prev -> next;
            
        // step 3: udpate connections
        newNode -> next = prev -> next;
        prev -> next = newNode;
    }
}

void deleteNode(Node* &head, int target, int pos) {
    // handle not found
    // handle tail update
    if(head == NULL)
        return;
    Node *temp = head;
    Node *prev = NULL;
    if(target == temp -> data && pos == 1) {
        head = head -> next;
        temp -> next = NULL;
        delete temp;
    } else {
        while(temp && temp -> data != target) {
            prev = temp;
            temp = temp -> next;
        }
        
        if(temp ==  NULL) {
            cout << "Not Found";
            return;
        }
        prev -> next = temp -> next;
        temp -> next = NULL;
        delete temp;
    }
}

int main ()
{
    // dynamic allocation
    Node *first = new Node (3);
    // cout << first->data << endl; // 3
    // cout << first->next << endl; // 0

    // static allocation
    Node second (5);
    // cout << second.data << endl; // 5
    // cout << second.next << endl; // 0
    
    // INSERTION
    
    Node *b = new Node(3);
    Node *head = b;
    Node *tail = b;
    
    // insertAtHead(head, 5);
    // insertAtHead(head, 7);
    // insertAtHead(head, 9);
    // insertAtHead(head, 11);
    
    // traverse(head); // 11 9 7 5 3
    
    insertAtTail(tail, 5);
    insertAtTail(tail, 7);
    insertAtTail(tail, 9);
    insertAtTail(tail, 11);
    
    traverse(head);

    return 0;
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/29-linked-list-i/code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
