-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindmiddlelinkedlist.c
More file actions
82 lines (68 loc) · 1.25 KB
/
findmiddlelinkedlist.c
File metadata and controls
82 lines (68 loc) · 1.25 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
typedef struct Node NODE;
#define null 0
struct Node
{
int data;
struct Node *next;
};
NODE *create()
{
NODE *start = null;
NODE *prev;
char ch = 'Y';
while (ch == 'Y')
{
NODE *newnode = (NODE *)malloc(sizeof(NODE));
scanf("%d", &newnode->data);
newnode->next = null;
if (start == null)
{
start = newnode;
}
else
{
prev->next = newnode;
}
prev = newnode;
printf("Continue?");
scanf(" %c", &ch);
}
return start;
}
void traversal(NODE *start)
{
while (start != null)
{
printf("%d ", start->data);
start = start->next;
}
printf("\n");
}
int getMiddle(NODE *start)
{
NODE *middle = start;
int cc = 0;
while (start != NULL)
{
cc++;
if (cc % 2 == 0)
{
middle = middle->next;
}
start = start->next;
}
return middle->data;
}
int main()
{
NODE *start = create();
traversal(start);
// swap_first_last(start);
// traversal(start);
//swap_contiguous(start);
traversal(start);
printf("%d", getMiddle(start));
// check_palindrome(start);
}