-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindMiddleNode.c
More file actions
63 lines (51 loc) · 1.11 KB
/
findMiddleNode.c
File metadata and controls
63 lines (51 loc) · 1.11 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct Node{
int value;
struct Node *next;
};
struct Node *head = NULL;
void findMiddleNode(struct Node *head){
struct Node *quick = head;
struct Node *slow = head;
if(head != NULL){
while(slow->next != NULL && quick->next->next != NULL){
slow = slow->next;
quick = quick->next->next;
}
if(quick->next != NULL)
printf("middle number is %d, %d\n", slow->value, slow->next->value);
else
printf("middle number is %d\n", slow->value);
}
else
printf("there is no list!!\n");
}
void push_value(int value){
struct Node *current = head;
while(current->next != NULL){
current = current->next;
}
struct Node *newnode = malloc(sizeof(struct Node));
newnode->value = value;
newnode->next = NULL;
current->next = newnode;
}
int main(void)
{
head = malloc(sizeof(struct Node));
head->value = 3;
push_value(2);
push_value(7);
push_value(8);
push_value(3);
push_value(6);
push_value(2);
push_value(1);
push_value(1);
push_value(8);
printf("Before Sorting : 3, 2, 7, 8, 3, 6, 2, 1, 1, 8\n");
findMiddleNode(head);
return 0;
}