-
Notifications
You must be signed in to change notification settings - Fork 0
83 lines (73 loc) · 2.08 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
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
using namespace std;
class node{
public:
int data;
node *next;
node(int value){
data = value;
next = NULL;
}
};
//node at end using recusion
node *createLL(int arr[] , int index, int size){
if(index==size){
return NULL;
}
node *temp = new node(arr[index]);
temp->next=createLL(arr, index+1,size);
return temp;
}
int main(){
// node *head = new node(10);
// cout<<head->data<<endl;
//Insertion at end by basic (Have more time complexity)
//steps -> Create tail ,Reach at the last node , create a node , link it
// node *tail = head;
// while(tail->next!=NULL){
// tail = tail->next;
// }
// node *temp = new node(20);
// tail->next =temp;
// tail=temp;
// cout<<temp->data<<endl;
//OPTIMised WAY to insert at end
//Steps - Create tail = head , check if head==null , else create new node and assign its value to
//tail ka next and then update the tail
// node *head = NULL;
// node *tail = NULL;
// int arr[3]={20,30,40};
// for(int i=0;i<3;i++){
// if(head==NULL){
// head= new node(arr[i]);
// tail=head;
// }
// else{
// tail->next = new node(arr[i]);
// tail = tail->next;
// }
// }
node*head = NULL;
int arr[3]={1,2,3};
head = createLL(arr,0,3);
//Insert at middle
//steps -> Get x and val ,make temp = head , move temp pointer to Xth node , create new node
// connect forward node , connect backward node
cout<<"Enter the value of X and value"<<endl;
int x,val;
cin>>x>>val;
node*temp1 = head;
x--;//Bcoz one node is used previously
while(x--){
temp1=temp1->next;
}
node *temp2 = new node(val);
temp2->next=temp1->next;//Forward node connected
temp1->next=temp2;//Backward node connected
//printing LL
node *temp =head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}