Now we will see how to insert a new node at position N in circular link list.
Algorithm for insertion at position N in circular link list:
In this algorithm, TAIL is pointer to the last node of the list and PTR is node to be inserted. The step are as below :- Create new node PTR
- Set the info field of PTR
- If list have no nodes i.e. (TAIL == NULL)
- If (POS == 1)
- Set PTR->NEXT = PTR
- Set TAIL = PTR
- Else
- Invalid POS. Node can't be inserted
- End If;
- If (POS == 1)
- Else if (POS == 1) (Now list is not empty also)
- Set PTR->NEXT = TAIL->NEXT
- Set TAIL->NEXT = PTR
- Else
- Set TEMP = TAIL->NEXT
- Traverse the list for (POS-1)th node of list into TEMP
- If TEMP == TAIL
- Set PTR->NEXT = TAIL->NEXT
- Set TAIL->NEXT = PTR
- Set TAIL = PTR
- Else
- Set PTR->NEXT = TEMP->NEXT
- Set TEMP->NEXT = PTR
- End If;
- End If;
Function to insert node at position N in circular link list:
void insertAtPosition(NODE **tail, int info, int pos){
NODE *ptr = (NODE*) malloc(sizeof(NODE));
ptr->info = info;
NODE *temp;
if(pos < 1){
printf("Error: Position can't be 0 or negative\n");
return;
}
if(*tail == NULL){
if(pos == 1){
ptr->next = ptr;
*tail = ptr;
}
else{
printf("\nList is empty. Invalid position\n");
}
}
else if(pos == 1){
ptr->next = (*tail)->next;
(*tail)->next = ptr;
}
else{
temp = (*tail)->next;
for( int i = 2; i < pos; i++){ temp = temp->next;
if(temp == (*tail)->next){
printf("\nError: Invalid position\n");
return;
}
}
if(temp == *tail){
ptr->next = (*tail)->next;
(*tail)->next = ptr;
*tail = ptr;
}
else{
ptr->next = temp->next;
temp->next = ptr;
}
}
}
Program to insert at position N in the singly link list:
#include <stdio.h>
#include <malloc.h>
struct node{
int info;
struct node *next;
};
typedef struct node NODE;
void insertAtFirst(NODE **, int);
void insertAtPosition(NODE **, int, int);
void traverse(NODE **);
int main(){
NODE *tail = NULL;
insertAtFirst(&tail, 1);
insertAtFirst(&tail, 3);
insertAtFirst(&tail, 4);
traverse(&tail);
insertAtPosition(&tail, 9, 0);
traverse(&tail);
insertAtPosition(&tail, 9, 1);
traverse(&tail);
insertAtPosition(&tail, 44, 3);
traverse(&tail);
insertAtPosition(&tail, 89, 6);
traverse(&tail);
insertAtPosition(&tail, 89, 8);
return 0;
}
void insertAtPosition(NODE **tail, int info, int pos){
NODE *ptr = (NODE*) malloc(sizeof(NODE));
ptr->info = info;
NODE *temp;
if(pos < 1){
printf("Error: Position can't be 0 or negative\n");
return;
}
if(*tail == NULL){
if(pos == 1){
ptr->next = ptr;
*tail = ptr;
}
else{
printf("\nList is empty. Invalid position\n");
}
}
else if(pos == 1){
ptr->next = (*tail)->next;
(*tail)->next = ptr;
}
else{
temp = (*tail)->next;
for( int i = 2; i < pos; i++){ temp = temp->next;
if(temp == (*tail)->next){
printf("\nError: Invalid position\n");
return;
}
}
if(temp == *tail){
ptr->next = (*tail)->next;
(*tail)->next = ptr;
*tail = ptr;
}
else{
ptr->next = temp->next;
temp->next = ptr;
}
}
}
void insertAtFirst(NODE **tail, int info){
NODE *ptr = (NODE*) malloc(sizeof(NODE));
ptr->info = info;
if(*tail == NULL){
ptr->next = ptr;
*tail = ptr;
}
else{
ptr->next = (*tail)->next;
(*tail)->next = ptr;
}
}
void traverse(NODE **tail){
NODE *temp;
temp = *tail;
if(*tail){
do{
printf("%d ", temp->next->info);
temp = temp->next;
} while(temp != *tail);
}
printf("\n");
}
4 3 1 Error: Position can't be 0 or negative 4 3 1 9 4 3 1 9 4 44 3 1 9 4 44 3 1 89 Error: Invalid position
Comments
Post a Comment