forked from iiitu-force/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlevalueLinkedlist.cpp
176 lines (157 loc) · 3.24 KB
/
middlevalueLinkedlist.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
class Node{
public:
int data;
Node*next;
Node(int data){
this->data=data;
next= NULL;
}
};
Node* takeInput(){
int data;
cin>>data;
Node *head= NULL;
Node *tail= NULL;
while(data != -1){
Node *newNode=new Node(data);
if(head==NULL){
head=newNode;
tail=newNode;
}else{
tail->next=newNode;
tail=tail->next;
}
cin>>data;
}
return head;
}
Node* takeInput2(Node* head,int data, int i){
Node* newNode=new Node(data);
if(head==NULL){
return head;
}
if(i==0){
newNode->next=head;
head=newNode;
return head;
}
else{
Node* temp= takeInput2(head->next,data,i-1);
head->next=temp;
return head;
}
}
void print(Node *head){
while (head!=NULL)
{
cout<<head->data<<" ";
head=head -> next;
}
}
Node* insertNode(Node *head,int i,int data){
Node *newNode=new Node(data);
int count=0;
Node *temp=head;
if(i==0){
newNode->next=head;
head=newNode;
return head;
}
while (temp!=NULL && count<i-1)
{
temp=temp->next;
count++;
}
if(temp!=NULL){
newNode->next=temp->next;
temp->next=newNode;
}
return head;
}
Node* deleteNode(Node *head,int i){
if(i==0){
head=head->next;
return head;
}
Node *temp=head;
int count=0;
while (temp!=NULL && count<i-1)
{
temp=temp->next;
count++;
}
if(temp!=NULL){
Node*a=temp->next;
temp->next=a->next;
}
return head;
}
Node* deleteNode2(Node* head,int i){
if(head==NULL){
return head;
}
if(i==0){
head=head->next;
return head;
}
else{
Node* temp=deleteNode2(head->next,i-1);
head->next=temp;
return head;
}
}
int lenghtofLinkedlist(Node *head){
Node*temp=head;
int count=0;
while (temp!=NULL)
{
temp=temp->next;
count++;
}
return count;
}
int getCount(Node *head){
if(head==NULL){
return 0;
}
return 1+ getCount(head->next);
}
void midleNode(Node* head){
if(head==NULL){
cout<<"Linked List is empty"<<endl;
return ;
}
Node* fast=head->next;
Node* slow=head;
while (fast!=NULL )
{
if(fast->next!=NULL){
fast=fast->next->next;
slow=slow->next;
}else{
break;
}
}
cout<<slow->data<<endl;
}
int main()
{
Node* head;
head=takeInput();
// int i;
// cin>>i;
midleNode(head);
// head=deleteNode2(head,i);
// head=takeInput2(head,data,i);
// print(head);
// int i,data;
// cin>>i;
// head=insertNode(head,i,data);
// head=deleteNode(head,i);
// cout<<lenghtofLinkedlist(head)<<endl;
// print(head);
return 0;
}