-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion6.c
156 lines (129 loc) · 2.95 KB
/
question6.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Find the middle node of a linked list
METHOD1:
counting the number of elements initially. Then finding the middle using that count.
Traverse the list again and then print that element
Time complexity: O(n)
Space complexity: O(1)
METHOD2:
maintain a hash table of addresses (what node has what address) and once count
is there, simply the middle node can be accessed by its address. This will help us not
traverse the linked list again to print the element.
Time complexity: O(n)
Space complexity: O(n)
METHOD3:
Maintain two pointers (slow and fast). fast will take two steps at a time and slow will
take one step. This way when first reaches the end, slow will be in the middle and we will have to
traverse the list only once
Time complexity: O(n)
Space complexity: O(1)
*/
//METHOD1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node{
int data;
struct node *next;
};
void printList(struct node *t){
if(t){
printf("%d --> ", t->data);
printList(t->next);
}
}
int main(){
struct node *head =(struct node *)malloc(sizeof(struct node));
int counter = 1;
struct node *t = head;
while(counter<=5){
t->data = counter*10;
if(counter == 5){
t->next = NULL;
}else{
t->next = (struct node *)malloc(sizeof(struct node));
}
t = t->next;
counter++;
}
t = head;
printList(t);
//finding the middle node
int middle = ceil(counter/2);
counter = 1;
while(t){
if(counter == 3){
printf("\nthe middle element is: %d\n", t->data);
break;
}
t=t->next;
counter++;
}
}
//=============================================================================================
//METHOD2
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node{
int data;
struct node *next;
};
int main(){
struct node *head =(struct node *)malloc(sizeof(struct node));
int counter = 1;
struct node *t = head;
while(counter<=5){
t->data = counter*10;
if(counter == 5){
t->next = NULL;
}else{
t->next = (struct node *)malloc(sizeof(struct node));
}
t = t->next;
counter++;
}
t = head;
//finding the middle node
int *hash = (int *)malloc(sizeof(int)*10);
counter = 1;
while(t){
hash[counter] = t->data;
t = t->next;
counter++;
}
int middle = ceil(counter/2);
printf("\nthe middle element is %d\n", *(hash+middle)); //@TODO facing prob here
}
//====================================================================
//METHOD3
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node{
int data;
struct node *next;
};
int main(){
struct node *head =(struct node *)malloc(sizeof(struct node));
int counter = 1;
struct node *t = head;
while(counter<=5){
t->data = counter*10;
if(counter == 5){
t->next = NULL;
}else{
t->next = (struct node *)malloc(sizeof(struct node));
}
t = t->next;
counter++;
}
t = head;
struct node *slow, *fast;
slow = fast = head;
while(fast && fast->next){
fast = fast->next->next;
slow = slow->next;
}
printf("\nthe middle element is %d\n", slow->data);
}