-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskipMdeleteN.cpp
More file actions
71 lines (70 loc) · 1.22 KB
/
skipMdeleteN.cpp
File metadata and controls
71 lines (70 loc) · 1.22 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
//This program takes ipnut into a linkedlist, and then takes
//two inputs M & N, and then it leaves the M nodes and
// deletes the further N nodes till we reach at the end.
#include<bits/stdc++.h>
using namespace std;
#include "node.cpp"
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=newnode;
}
cin>>data;
}
return head;
}
void print(node *head){
while(head!=NULL){
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}
node* skipMdeleteN(node *head,int M, int N){
int m=M;
int n=N;
node *temp=head;
while(temp!=NULL){
m=M;
n=N;
while(temp!=NULL && (m-1)>0){
cout<<temp->data<<"->";
temp=temp->next;
--m;
}
if(temp==NULL)
return head;
while(temp!=NULL && n>0){
if(temp->next!=NULL){
cout<<"("<<temp->next->data<<")"<<"->";
temp->next=temp->next->next;
temp=temp->next;
--n;
}
else
return head;
}
if(temp==NULL)
return head;
}
}
int main(int argc, char const *argv[])
{
int M,N;
node *head=takeinput();
print(head);
cin>>M>>N;
head=skipMdeleteN(head,M,N);
print(head);
return 0;
}