1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class node {
5
+
6
+ public:
7
+ int value ;
8
+ node* next ;
9
+
10
+ node (int data){
11
+ value = data;
12
+ next = NULL ;
13
+ }
14
+ };
15
+
16
+ // inserting at the head
17
+ void insertAtHead ( node* &head, int val){ // take by reference as we have to change the value of head
18
+ node* new_node = new node (val);
19
+ new_node-> next = head ;
20
+ head = new_node;
21
+ }
22
+
23
+ // inserting at the tail
24
+ void insertAtTail (node* &head, int val){
25
+
26
+ node* new_node = new node (val) ;
27
+ node* temp = head;
28
+
29
+ while (temp->next !=NULL ){
30
+ temp = temp->next ;
31
+
32
+ }
33
+ // temp has reached last node
34
+ temp->next = new_node; // already points to NULL by the constuctor
35
+
36
+ }
37
+
38
+ // to insert the kth node
39
+ void insertAtPosition (node* head, int val, int pos){
40
+
41
+ if (pos ==0 ) {
42
+ insertAtHead ( head, val) ;
43
+ return ;
44
+ }
45
+
46
+ node* new_node = new node (val) ;
47
+ node* temp = head;
48
+ int cur_pos = 0 ;
49
+
50
+ while (cur_pos!=pos-1 ){
51
+ temp = temp->next ;
52
+ cur_pos++;
53
+ }
54
+
55
+ // temp is pointing to pos-1
56
+
57
+ new_node->next = temp->next ;
58
+ temp->next = new_node;
59
+
60
+ }
61
+
62
+ // to update at kth node
63
+ void updateAtPosition (node* head, int val, int pos){
64
+
65
+ node* temp = head;
66
+ int curr_pos = 0 ;
67
+
68
+ while (curr_pos!=pos){
69
+
70
+ temp = temp->next ;
71
+ curr_pos++; }
72
+
73
+ temp->value = val;
74
+ }
75
+
76
+ // to delete at head
77
+ void deleteAtHead (node* &head){
78
+
79
+ node* temp = head;
80
+
81
+ head = temp->next ;
82
+ delete temp;
83
+ }
84
+
85
+ // to delete at the last
86
+ void deleteAtTail (node* head){
87
+
88
+ node* second_last = head;
89
+ while (second_last->next ->next != NULL ){
90
+ second_last= second_last->next ;
91
+ }
92
+
93
+ node* temp = second_last->next ;
94
+ second_last->next = NULL ;
95
+
96
+ delete temp;
97
+
98
+ }
99
+
100
+ // to print all the nodes
101
+ void display (node* head){
102
+ node* temp= head;
103
+
104
+ while (temp!=NULL ){
105
+ cout << temp->value << " ->" ;
106
+ temp = temp->next ;
107
+ }
108
+ cout << " NULL" << endl;
109
+ }
110
+
111
+ // to delete at kth position
112
+ void deleteAtPosition (node* &head, int pos){
113
+
114
+ if (pos==0 ){
115
+ deleteAtHead (head);
116
+ return ;
117
+ }
118
+
119
+ node* prev = head;
120
+ int curr_pos = 0 ;
121
+
122
+ while (curr_pos!=pos-1 ){
123
+ prev= prev->next ;
124
+ curr_pos++;
125
+ }
126
+ node* temp = prev->next ;
127
+ prev->next = prev->next ->next ;
128
+ delete temp;
129
+
130
+ }
131
+
132
+ int main (){
133
+
134
+ // node *n = new node(1);
135
+
136
+ // cout << n->value << " " << n->next;
137
+
138
+ node* head = NULL ; // empty linked list
139
+
140
+ insertAtHead (head,2 );
141
+ insertAtHead (head,1 );
142
+ insertAtTail (head,4 );
143
+ insertAtPosition (head,5 ,1 );
144
+
145
+ display (head);
146
+
147
+ updateAtPosition (head,7 ,1 );
148
+
149
+ display (head);
150
+
151
+ // deleteAtHead(head);
152
+ // deleteAtTail(head);
153
+ deleteAtPosition (head,2 ) ;
154
+
155
+ display (head);
156
+
157
+
158
+ return 0 ;
159
+ }
0 commit comments