Skip to content

Added Code for Insertion in Singly List #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions Linked List/02_Insert_in_Singly_Linked_List.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
To insert a node in a singly linked list in C++, you can follow the procedures:
inserting at the beginning, inserting at the end, and inserting at a specific position. Here are the implementations for each scenario:

1. Insert at the Beginning
To insert a node at the beginning of a singly linked list:

#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int data) : data(data), next(nullptr) {}
};

class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}

void insertAtBeginning(int data) {
Node* newNode = new Node(data);
newNode->next = head;
head = newNode;
}

void printList() {
Node* current = head;
while (current) {
cout << current->data << " -> ";
current = current->next;
}
cout << "None" << endl;
}
};

// Example usage:
int main() {
LinkedList ll;
ll.insertAtBeginning(3);
ll.insertAtBeginning(2);
ll.insertAtBeginning(1);
ll.printList();
return 0;
}

2. Insert at the End
To insert a node at the end of a singly linked list:


#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int data) : data(data), next(nullptr) {}
};

class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}

void insertAtEnd(int data) {
Node* newNode = new Node(data);
if (!head) {
head = newNode;
return;
}
Node* last = head;
while (last->next) {
last = last->next;
}
last->next = newNode;
}

void printList() {
Node* current = head;
while (current) {
cout << current->data << " -> ";
current = current->next;
}
cout << "None" << endl;
}
};

// Example usage:
int main() {
LinkedList ll;
ll.insertAtEnd(1);
ll.insertAtEnd(2);
ll.insertAtEnd(3);
ll.printList();
return 0;
}
3. Insert at a Specific Position
To insert a node at a specific position in a singly linked list:


#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int data) : data(data), next(nullptr) {}
};

class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}

void insertAtPosition(int data, int position) {
Node* newNode = new Node(data);
if (position == 0) {
newNode->next = head;
head = newNode;
return;
}
Node* current = head;
for (int i = 0; i < position - 1; ++i) {
if (!current) {
throw out_of_range("Position out of bounds");
}
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}

void printList() {
Node* current = head;
while (current) {
cout << current->data << " -> ";
current = current->next;
}
cout << "None" << endl;
}
};

// Example usage:
int main() {
LinkedList ll;
ll.insertAtEnd(1);
ll.insertAtEnd(2);
ll.insertAtEnd(4);
ll.insertAtPosition(3, 2);
ll.printList();
return 0;
}
In this insertAtPosition function, if the specified position is out of bounds, an out_of_range exception is thrown to handle invalid positions properly. This ensures the robustness of the implementation.