//inserting in the middle from array elements into first linkedlist. //please upvote if you find it helpful :) #include<stdio.h> #include<stdlib.h> #include<math.h> struct node{ int data; struct node* next; struct node* prev; }; struct node* newnode(int data){ struct node* head = (struct node*)malloc(sizeof(struct node)); head->data=data; head->next=NULL; head->prev=NULL; return head; } void display(struct node* head){ struct node* temp=head; while(temp!=NULL){ printf("%d ",temp->data); temp=temp->next; } printf("\n"); } struct node* insertnext(int arr[], int n){ struct node* head = newnode(arr[0]); struct node* temp=head; for(int i=1;i<n;i++){ struct node* neww = newnode(arr[i]); temp->next=neww; neww->prev=temp; temp=neww; } return head; } // first-> doubly linked list, size n // brr-> array elements(size m) to be inserted in middle one by one void insertmiddle(struct node* first, int n, int brr[], int m){ for(int i=1;i<=m;i++){ int mid=ceil((n+i)/2); struct node* temp=first; struct node* meww= newnode(brr[i-1]); struct node* temp3; for(int j=0;j<mid;j++){ temp=temp->next; } meww->next=temp; meww->prev=temp->prev; temp3=temp->prev; temp3->next=meww; temp->prev=meww; display(first); } } int main(){ int n,m,i,j; scanf("%d",&n); int arr[n]; for(i=0;i<n;i++){ scanf("%d",&arr[i]); } scanf("%d",&m); int brr[m]; for(j=0;j<m;j++){ scanf("%d",&brr[j]); } struct node* first= insertnext(arr,n); display(first); insertmiddle(first,n,brr,m); return 0; } //input: 4 1 2 3 4 3 5 6 7 //output: 1 2 3 4 1 2 5 3 4 1 2 5 6 3 4 1 2 5 7 6 3 4