-
Notifications
You must be signed in to change notification settings - Fork 1
/
mid_linkedlist.c
59 lines (52 loc) · 973 Bytes
/
mid_linkedlist.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int value;
struct node* next;
}node;
node* Start = NULL;
int find_mid(){
node* head = Start;
node* tail= Start;
while(tail && tail->next){
head = head->next;
tail = tail->next->next;
}
return head->value;
}
void insert(){
int entry;
node* temp = NULL;
printf("Enter value: ");
scanf("%d", &entry);
temp = (node*)malloc(sizeof(node));
temp->value = entry;
temp->next = NULL;
if(!Start)
Start = temp;
else{
node* head = Start;
while(head->next){head=head->next;};
head->next = temp;
}
}
void display(){
node* head = Start;
while(head){
printf("%d->", head->value);
head = head->next;
}
}
int main(){
int num, i, entry;
printf("Enter number of elements you want to insert in the linked list: ");
scanf("%d", &num);
printf("Number Entered: \n");
printf("%d", num);
for(i = 0; i < num; i++){
insert();
}
display();
printf("Mid Value: %d", find_mid());
return 0;
}