-
Notifications
You must be signed in to change notification settings - Fork 618
/
insertion_in_linked_list.cpp
165 lines (136 loc) · 2.61 KB
/
insertion_in_linked_list.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
/*
Insertion into a singly-linked list has three cases:
1. Inserting a new node before the head (at the beginning)
• Update the next pointer of new node, to point to the current head.
• Update head pointer to point to the new node.
2. Inserting a new node after the tail (at the end of the list)
• New nodes next pointer points to NULL.
• Last nodes next pointer points to the new node.
3. Inserting a new node at the middle of the list (random location)
• If we want to add an element at position 3 then we stop at position 2. That means we
traverse 2 nodes and insert the new node. For simplicity let us assume that the
second node is called position node. The new node points to the next node of the
position where we want to add this node.
• Position node’s next pointer now points to the new node.
*/
#include <iostream>
using namespace std;
class node{
public:
int data;
node* next;
//constructor
node(int x){
data = x;
next = nullptr;
}
};
class LinkedList{
public:
node* head;
node* tail;
//constructor
LinkedList(){
head = nullptr;
tail = nullptr;
}
void insertion_at_head(int x){
// node create
node* n = new node(x);
//only one node
if(head == nullptr){
head = n;
tail = n;
}
else{
n->next = head;
head = n;
}
}
void insertion_at_tail(int x){
node* n = new node(x);
//only one node
if(head == nullptr){
head = n;
tail = n;
}
else{
tail->next = n;
tail = n;
}
}
void insertion_at_x(int x, int pos){
node* n = new node(x);
if(pos<=0){
insertion_at_head(x);
}
else if(pos>length()){
insertion_at_tail(x);
}
else{
node* temp = head;
int move = 1;
while(move < pos-1){
move++;
temp = temp->next;
}
n->next = temp->next;
temp->next = n;
}
}
int length(){
node* temp = head;
int l = 0;
while(temp!=nullptr){
l++;
temp = temp->next;
}
return l;
}
void print(){
node* temp = head;
while(temp!=nullptr){
cout << temp->data<<" ";
temp = temp->next;
}
}
};
int main() {
LinkedList l;
int n;
cin >> n;
for(int i=1;i<n;i++){
l.insertion_at_head(i);
}
l.print();
cout<<"\n";
int ele;
cin >> ele;
l.insertion_at_tail(ele);
l.print();
cout<<"\n";
int pos;
cin >> ele >> pos;
l.insertion_at_x(ele,pos);
l.print();
cout<<"\n";
cout<< l.length();
return 0;
}
/*
Test case :
Input :
5
6
7 3
Output :
4 3 2 1
4 3 2 1 6
4 3 7 2 1 6
6
Time Complexity :
Insertion at start : O(1)
Insertion at end : O(N)
Insertion at random position : O(N)
Space Complexity : O(N)
*/