-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertioninDLL.cpp
More file actions
107 lines (79 loc) · 1.77 KB
/
insertioninDLL.cpp
File metadata and controls
107 lines (79 loc) · 1.77 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*Author : KRISH JHA */
/*scholar id : 2312119*/
#include<bits/stdc++.h>
using namespace std;
class Node{
public :
int data;
Node* next;
Node* prev;
Node(int data){
this -> data = data;
this -> next = NULL;
this -> prev = NULL;
}
};
void prepend(Node* &head , int data){
Node* newNode = new Node(data);
newNode -> next = head;
if(head != NULL){
head -> prev = newNode;
}
head = newNode;
}
void append(Node* &head , int data){
Node* newNode = new Node(data);
if(head == NULL){
head = newNode;
}
else{
Node* temp = head;
while(temp -> next){
temp = temp -> next;
}
temp -> next = newNode;
newNode -> prev = temp;
}
}
void insertAtk(Node* &head , int pos , int data){
Node* newNode = new Node(data);
if(pos == 1){
prepend(head , data);
}
else{
Node* temp = head;
int cnt = 1;
while(temp != NULL && cnt != pos - 1){
temp = temp -> next;
}
newNode -> prev = temp;
newNode -> next = temp -> next;
temp -> next = newNode;
newNode -> next -> prev = newNode;
}
}
void printDLL(Node* &head){
Node* temp = head;
while(temp != NULL){
cout << temp -> data << ' ';
temp = temp -> next;
}
cout << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Node* head = new Node(1);
printDLL(head);
prepend(head , 2);
printDLL(head);
append(head , 3);
printDLL(head);
insertAtk(head , 2 , 4);
printDLL(head);
return 0;
}