Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive method that finds the middle element of a linked list. That is

ID: 440045 • Letter: W

Question

Write a recursive method that finds the middle element of a linked list. That is, if the list has n elements, the method returns the n/2 element. The method may not contain a loop, may not call any other methods other than itself recursively, and is not given the size of the list.

Explanation / Answer

Method 1: Traverse the whole linked list and count the no. of nodes. Now traverse the list again till count/2 and return the node at count/2. Method 2: Traverse linked list using two pointers. Move one pointer by one and other pointer by two. When the fast pointer reaches end slow pointer will reach middle of the linked list. #include #include /* Link list node */ struct node { int data; struct node* next; }; /* Function to get the middle of the linked list*/ void printMiddle(struct node *head) { struct node *slow_ptr = head; struct node *fast_ptr = head; if(head!=NULL) { while((fast_ptr->next)!=NULL && (fast_ptr->next->next)!=NULL) { fast_ptr = fast_ptr->next->next; slow_ptr = slow_ptr->next; } printf("The middle element is [%d]",slow_ptr->data); } } void push(struct node** head_ref, int new_data) { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the data */ new_node->data = new_data; /* link the old list off the new node */ new_node->next = (*head_ref); /* move the head to point to the new node */ (*head_ref) = new_node; } /* Drier program to test above function*/ int main() { /* Start with the empty list */ struct node* head = NULL; push(&head, 20); push(&head, 4); push(&head, 15); printMiddle(head); return 0; } Method 3: Initialize mid element as head and initialize a counter as 0. Traverse the list from head, while traversing increment the counter and change mid to mid->next whenever the counter is odd. So the mid will move only half of the total length of the list. Thanks to Narendra Kangralkar for suggesting this method. #include #include /* Link list node */ struct node { int data; struct node* next; }; /* Function to get the middle of the linked list*/ void printMiddle(struct node *head) { int count = 0; struct node *mid = head; while (head != NULL) { /* update mid, when 'count' is odd number */ if (count & 1) mid = mid->next; ++count; head = head->next; } /* if empty list is provided */ if (mid != NULL) printf(" The middle element is %d ", mid->data); } void push(struct node** head_ref, int new_data) { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the data */ new_node->data = new_data; /* link the old list off the new node */ new_node->next = (*head_ref); /* move the head to point to the new node */ (*head_ref) = new_node; } /* Drier program to test above function*/ int main() { /* Start with the empty list */ struct node* head = NULL; push(&head, 20); push(&head, 4); push(&head, 30); printMiddle(head); return 0; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote