You are given a single linked list, L, and another linked list, P, containing in
ID: 3620055 • Letter: Y
Question
You are given a single linked list, L, and another linked list, P, containing integers, sorted in ascending order. The operation print_lots(L,P) will print the elements in L that are in positions specified by P. For instance, if P = 1, 3, 4, 6, the first, third, fourth, and sixth elements in L are printed. Write the routine print_lots(L,P). You should use only the basic list operations.Explanation / Answer
struct list { int pos; struct list *next; }; //here is function using above struct. //assuming L and P are list passed to it. print_lots(struct list *L,struct list *P) { struct list *ptr,*ptr1; int pos; ptr = P ; int current; while(ptr != NULL) { pos = ptr->pos; ptr1 = L; current =1; /* scan L upto pos */ while(ptr1 != NULL && current != pos) { ptr1=ptr1->next; current++; } /* Print number at pos given by above loop */ if( pos == current && ptr != NULL) { printf("%d ",ptr->pos); } ptr = ptr->next; } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.