Simple Program Unit 2
Simple Program Unit 2
RA2011003010008
CSE A1
Question:
3. Write a program in C to print the element at the middle node of a Linked List.
Approach:
Pointers are used for traversing to find the middle node of the list.
Follow Up Question:
Q) What happens if the number of elements in the list is even?
A) n/2th element is found as the middle element, where “n” is the total number of elements.
Scalability:
User can enter the Size of the list and can define any value.
Code:
#include<stdio.h>
#include<stdlib.h>
struct Node
int data;
};
//Function for finding the middle node of the list
if(head!=NULL)
first=first->next;
last=last->next->next;
new_node->data = data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
int i,t;
scanf("%d",&t);
for(i=t;i>0;i--)
int x;
scanf("%d",&x);
push(&head,x);
middle(head);
return 0;