void insert_mid() { int pos,i; struct node *new_node,*current,*temp,*temp1; new_node=(struct node *)malloc(sizeof(struct node)); printf("nEnter the data : "); scanf("%d",&new_node->data); new_node->next=NULL; st : printf("nEnter the position : "); scanf("%d",&pos); if(pos>=(length()+1)) { printf("nError : pos > length "); goto st; } if(start==NULL) { start=new_node; current=new_node; } else { temp = start; for(i=1;i< pos-1;i++) { temp = temp->next; } temp1=temp->next; temp->next = new_node; new_node->next=temp1; } }
Explanation :
Step 1 : Get Current Position Of “temp” and “temp1” Pointer.
temp = start; for(i=1;i< pos-1;i++) { temp = temp->next; }
Step 2 :
temp1=temp->next;
Step 3 :
temp->next = new_node;
Step 4 :
new_node->next = temp1
No comments:
Post a Comment