forked from shamilkeheliya/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinekdListcfile.c
71 lines (58 loc) · 1 KB
/
LinekdListcfile.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
//skelton for every node of linked list
struct Node
{
int data;
struct Node *next;
};
//function to print all the elements in linked list
void display(struct Node *n)
{
while(n != NULL)
{
printf("%d\t",n->data);
n = n->next;
}
}
void findMiddle(struct Node *head)
{
struct Node *slow = head;
struct Node *fast = head;
if(head != NULL)
{
while(fast!=NULL && fast->next!=NULL)
{
fast = fast->next->next;
slow = slow->next;
}
}
printf("Middle element is : %d",slow->data);
}
int main()
{
struct Node *nd[10] = {NULL};
//memory allocation
for(int i=0; i<10; i++)
{
nd[i] = (struct Node*)malloc(sizeof(struct Node));
}
//assignning values to Nodes
for(int i=0; i<10; i++)
{
printf("Insert number %d: ",i+1);
scanf("%d",&(nd[i]->data));
}
//linking Nodes
for(int i=0; i<9; i++)
{
nd[i]->next = nd[i+1];
}
nd[9]->next = NULL;
//calling display
display(nd[0]);
printf("\n");
findMiddle(nd[0]);
printf("\n");
return 0;
}