forked from v100901/hackoctoberfest2020__
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsert Node Linked List(Recursive)
71 lines (68 loc) · 1.01 KB
/
Insert Node Linked List(Recursive)
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
```#include<iostream>
using namespace std;
class node{
public:
int data;
node*next;
node(int data){
this->data=data;
this->next=NULL;
}
};
node*takeinput(){
int data;
node*head=NULL;
node*tail=NULL;
cin>>data;
while(data!=-1){
node*n=new node(data);
if(head==NULL){
head=n;
tail=n;
}
else{
tail->next=n;
tail=n;
}
cin>>data;
}
return head;
}
void print(node*head){
while(head!=NULL){
cout<<head->data<<" ";
head=head->next;
}
}
void insertrecursive(node*head,int pos,int no){
if(head==NULL)
return;
else{
if(pos==0){
node*n=new node(no);
n->next=head->next;
head->next=n;
return;
}
else{
head=head->next;
insertrecursive(head,pos-1,no);
}
}
}
int main(){
int one,pos,no;
node*head=takeinput();
print(head);
cout<<"Press 1 to insert node";
cin>>one;
if(one==1){
cout<<"enter the position to be inserted at: ";
cin>>pos;
cout<<"enter the number to be inserted: ";
cin>>no;
insertrecursive(head,pos,no);
print(head);
}
return 0;
}```