-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddle_of_ll.cpp
79 lines (72 loc) · 1.44 KB
/
middle_of_ll.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
#include<iostream>
using namespace std;
class node{
public:
node* insert1(int,node *start);
node* mid(node *start);
void disp1(node *start);
node *next;
int data;
};
node* node::insert1(int a,node *start){
if(start == NULL)
{
node *temp = new node();
temp->next = NULL;
temp->data = a;
start = temp;
return start;
}
node *temp = new node();
temp->next = NULL;
temp->data = a;
node *iter = start;
while(iter->next !=NULL)
{
iter = iter->next;
}
iter->next = temp;
return start;
}
node* node::mid(node *start){
if(start == NULL)
{
cout<<"ll is empty";
return start;
}
node *fast = start;
node *slow = start;
while(fast->next !=NULL)
{
fast = fast->next;
if(fast->next == NULL)
{
cout<<" The middle ele is "<<slow->data<<" ";
slow = slow->next;
cout<<slow->data;
return start;
}
fast = fast->next;
slow = slow->next;
}
cout<<" The middle ele is "<<slow->data;
return start;
}
void node::disp1(node *start){
while(start->next != NULL)
{
cout<<start->data<<"->";
start = start->next;
}
cout<<start->data;
}
int main(){
node b;
node *start=NULL;
start=b.insert1(10,start);
start=b.insert1(20,start);
start=b.insert1(30,start);
start=b.insert1(40,start);
b.disp1(start);
start=b.mid(start);
}