-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOLUTION TO ISSUE #13.txt
55 lines (41 loc) · 1.35 KB
/
SOLUTION TO ISSUE #13.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
SOLUTION TO ISSUE #13
Program to find middle element of SINGLE LINKED LIST using C Language.
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));
struct Node* fourth = (struct Node*)malloc(sizeof(struct Node));
printf("Enter value for the first node: ");
scanf("%d", &head->data);
head->next = second;
printf("Enter value for the second node: ");
scanf("%d", &second->data);
second->next = third;
printf("Enter value for the third node: ");
scanf("%d", &third->data);
third->next = fourth;
printf("Enter value for the fourth node: ");
scanf("%d", &fourth->data);
fourth->next = NULL;
struct Node* slow_ptr = head;
struct Node* fast_ptr = head;
if (head != NULL) {
while (fast_ptr != NULL && fast_ptr->next != NULL) {
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}
printf("The middle element is %d\n", slow_ptr->data);
}
free(head);
free(second);
free(third);
free(fourth);
return 0;
}