-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp-data-structures-linked-list-find-middle-element.cpp
102 lines (75 loc) · 1.53 KB
/
cpp-data-structures-linked-list-find-middle-element.cpp
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
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
}
*head = new Node;
void create(int A[], int n) // creating a doubly linked list from an array
{
Node* t, * tail;
int i;
head->data = A[0];
head->next = NULL;
tail = head;
for (i = 1; i < n; i++)
{
t = new Node;
t->data = A[i];
t->next = NULL;
tail->next = t;
tail = t;
}
}
void Display(struct Node* p) // display the linked list elements
{
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int Length(struct Node* p)
{
int len = 0;
while (p)
{
len++;
p = p->next;
}
return len;
}
void middleNode2(struct Node* p)
{
struct Node* q;
p = q = head;
while (q) // as long as q is not NULL
{
q = q->next; // sets q one step ahead
if(q) q = q->next; // sets q two steps ahead
if (q) p = p->next; // p follows q half as many steps behind q
}
cout << "Middle Element (Method-II): " << p->data << endl;
}
void middleNode1(Node* p) {
int length = 0;
while (p) { // incrementing p to length of linked list
length++;
p = p->next;
}
int index = (int)ceil(length / 2.0); // using half the length for middle rounded up with ceil
Node* q = head;
for (int i = 0; i < index - 1; i++) { // setting q to half the length of p
q = q->next;
}
cout << "Middle Element (Method-I): " << q->data << endl;
}
int main() {
int A[] = { 3,5,7,9,11,13,15 };
create(A, 7);
Display(head);
middleNode1(head);
middleNode2(head);
return 0;
}