To insert a new node in the given linked list can be performed in 3 positions:
- At beginning of linked list
- At given node
- At the end of linked list
To insert a node at beginning of a Linked List:
- Create a new node
- Copy the address of actual first node into address field of new node
- Make the new node as the Head of the Linked List.
void ins_begin(){
struct node *newNode;
if(head==NULL){
printf("List is empty.\n");
}else{
newNode=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&newNode->data);
newNode->next=head;
head=newNode;
}
}
struct node *newNode;
if(head==NULL){
printf("List is empty.\n");
}else{
newNode=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&newNode->data);
newNode->next=head;
head=newNode;
}
}
To insert a new node at given node of a Linked List:
- Create new node
- Move a temporary pointer to the given node if exists otherwise skip the process.
- If given node exists then copy the address present temporary node next field into new node address field
- Finally, change the address field of temporary node with new node.
void ins_atgiven(){
int nodeval;
struct node *newNode,*temp;
if(head==NULL){
printf("List is empty.\n");
}else{
printf("Enter node value:\n");
scanf("%d",&nodeval);
newNode=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&newNode->data);
temp=head;
while(temp->data!=nodeval)
temp=temp->next;
if(temp->data==nodeval){
newNode->next=temp->next;
temp->next=newNode;
}
}
}
int nodeval;
struct node *newNode,*temp;
if(head==NULL){
printf("List is empty.\n");
}else{
printf("Enter node value:\n");
scanf("%d",&nodeval);
newNode=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&newNode->data);
temp=head;
while(temp->data!=nodeval)
temp=temp->next;
if(temp->data==nodeval){
newNode->next=temp->next;
temp->next=newNode;
}
}
}
To insert a node at end of a Linked List:
- Create a new node
- Move temporary node pointer to last of linked list.
- Change the address field of temporary node with new node.
- Make the address field of new node with NULL to show the end of linked list.
void ins_end(){
struct node *newNode,*temp;
if(head==NULL){
printf("List is empty.\n");
}else{
newNode=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&newNode->data);
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newNode;
newNode->next=NULL;
}
}
struct node *newNode,*temp;
if(head==NULL){
printf("List is empty.\n");
}else{
newNode=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&newNode->data);
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newNode;
newNode->next=NULL;
}
}
Click on the below button for implementation of Singly Linked List with it's operations.
Related Programs
- Create Singly Linked List with N number of nodes and display.
- Perform insertion operation on Singly Linked List.
- Perform deletion operation on Singly Linked List.
- Reverse of Singly Linked List.
- Move last node of Singly Linked List to first.
- Swap first and last nodes in a Singly Linked List.
- Find the length of Singly Linked List.
- Find middle node of Singly Linked List