How do you insert a node after a position temperature in singly linked list?

Insert a node at a specific position in a linked list

Given a singly linked list, a position and an element, the task is to write a program to insert that element in a linked list at a given position.

Examples:

Input: 3->5->8->10, data = 2, position = 2 Output: 3->2->5->8->10 Input: 3->5->8->10, data = 11, position = 5 Output: 3->5->8->10->11
Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: To insert a given data at a specified position, the below algorithm is to be followed:

  • Traverse the Linked list upto position-1 nodes.
  • Once all the position-1 nodes are traversed, allocate memory and the given data to the new node.
  • Point the next pointer of the new node to the next of current node.
  • Point the next pointer of current node to the new node.

Below is the implementation of the above algorithm.

C++




// C++ program for insertion in a single linked
// list at a specified position
#include
using namespace std;
// A linked list Node
struct Node {
int data;
struct Node* next;
};
// Size of linked list
int size = 0;
// function to create and return a Node
Node* getNode[int data]
{
// allocating space
Node* newNode = new Node[];
// inserting the required data
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// function to insert a Node at required position
void insertPos[Node** current, int pos, int data]
{
// This condition to check whether the
// position given is valid or not.
if [pos < 1 || pos > size + 1]
cout next;
}
size++;
}
}
// This function prints contents
// of the linked list
void printList[struct Node* head]
{
while [head != NULL] {
cout next;
}
cout 5->8->10
Node* head = NULL;
head = getNode[3];
head->next = getNode[5];
head->next->next = getNode[8];
head->next->next->next = getNode[10];
size = 4;
cout data = item; ptr->next = head; head = ptr; printf["\nNode inserted successfully!!\n"]; } } void insertNode[int item] { struct node *ptr = [struct node *] malloc [sizeof[struct node]]; struct node *temp; int i,loc; if[ptr == NULL] { printf["\nOVERFLOW"]; } else { printf["\nEnter the location:\n"]; scanf["%d",&loc]; ptr->data = item; temp=head; for[i=0;inext; if[temp == NULL] { printf["\nElement can't be inserted.\n"]; return; } } ptr ->next = temp ->next; temp ->next = ptr; printf["\nNode inserted successfully!!"]; } }

#include #include void insertNode[int]; void createNode[int]; struct node { int data; struct node *next; }; struct node *head; void main [] { int choice,item,loc; do { printf["\nEnter the element to insert:\n"]; scanf["%d",&item]; if[head == NULL] { createNode[item]; } else { insertNode[item]; } printf["\nPress 0 to insert more.\n"]; scanf["%d",&choice]; }while[choice == 0]; } void createNode[int item] { struct node *ptr = [struct node *]malloc[sizeof[struct node *]]; if[ptr == NULL] { printf["\nOVERFLOW\n"]; } else { ptr->data = item; ptr->next = head; head = ptr; printf["\nNode inserted successfully!!\n"]; } } void insertNode[int item] { struct node *ptr = [struct node *] malloc [sizeof[struct node]]; struct node *temp; int i,loc; if[ptr == NULL] { printf["\nOVERFLOW"]; } else { printf["\nEnter the location:\n"]; scanf["%d",&loc]; ptr->data = item; temp=head; for[i=0;inext; if[temp == NULL] { printf["\nElement can't be inserted.\n"]; return; } } ptr ->next = temp ->next; temp ->next = ptr; printf["\nNode inserted successfully!!"]; } }

Output

Enter the element to insert: 2 Node inserted successfully!! Press 0 to insert more. 0 Enter the element to insert: 4 Enter the location: 0 Node inserted successfully!! Press 0 to insert more. 0 Enter the element to insert: 3 Enter the location: 1 Node inserted successfully!! Press 0 to insert more. 0 Enter the element to insert: 1 Enter the location: 3 Element can't be inserted. Press 0 to insert more.
Please Share

inserting a node at the end of a linked list

The new node will be added at the end of the linked list.



Linked List Operations: Traverse, Insert and Delete

In this tutorial, you will learn different operations on a linked list. Also, you will find implementation of linked list operations in C/C++, Python and Java.

There are various linked list operations that allow us to perform different actions on linked lists. For example, the insertion operation adds a new element to the linked list.

Here's a list of basic linked list operations that we will cover in this article.

  • Traversal - access each element of the linked list
  • Insertion - adds a new element to the linked list
  • Deletion - removes the existing elements
  • Search - find a node in the linked list
  • Sort - sort the nodes of the linked list

Before you learn about linked list operations in detail, make sure to know about Linked List first.

Things to Remember about Linked List

  • head points to the first node of the linked list
  • next pointer of the last node is NULL, so if the next current node is NULL, we have reached the end of the linked list.

In all of the examples, we will assume that the linked list has three nodes 1 --->2 --->3 with node structure as below:

struct node { int data; struct node *next; };

Video liên quan

Bài Viết Liên Quan

Bài mới nhất

Chủ Đề