Repository navigation
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
178 lines (138 loc) · 3.17 KB
/
Copy pathlinked_list.cpp
File metadata and controls
178 lines (138 loc) · 3.17 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
this->data = val;
this->next = NULL;
}
};
void append(node** head, int val){
if(*head == NULL){
*head = new node(val);
return;
}
node* pcrawl = *head;
while(pcrawl->next!=NULL){
pcrawl = pcrawl->next;
}
node* temp_node = new node(val);
pcrawl->next = temp_node;
return;
}
void insert_at_beginning(node** head, int val){
if(*head == NULL){append(head, val); return;}
node* temp_node = new node(val);
temp_node->next = *head;
*head = temp_node;
return;
}
void insert_after(node** head, node* temp, int val){
if(*head == NULL){
cout<<"no elements inside\n";
return;
}
if(temp == NULL){
cout<<"enter valid node pointer\n";
return;
}
node* pcrawl = *head;
node* prev = pcrawl;
while(pcrawl != temp){
prev = pcrawl;
pcrawl=pcrawl->next;
}
if(pcrawl==NULL){
cout<<"no such node pointer found\n";
return;
}
prev = pcrawl;
pcrawl = pcrawl->next;
node* temp_node = new node(val);
temp_node->next = pcrawl->next;
prev->next = temp_node;
temp_node->next=pcrawl;
return;
}
void delete_key(node** head, int val){
if(*head == NULL){
cout<<"no elements inside\n";
return;
}
node* pcrawl = *head;
node* prev = pcrawl;
while(pcrawl->data != val){
prev = pcrawl;
pcrawl=pcrawl->next;
}
if(pcrawl==NULL){
cout<<"no such val(node) found\n";
return;
}
if(prev == pcrawl){ // if the first element is the node with data 'val'.
*head = prev->next;
prev->next = NULL;
delete(prev);
return;
}
prev->next = pcrawl->next;
pcrawl->next = NULL;
delete(pcrawl);
return;
}
void nth_from_end(node** head, int n){
if(n<1){
cout<<"enter valid n\n";
return;
}
if(*head == NULL){
cout<<"no elements inside\n";
return;
}
node* pcrawl = *head;
node* prev = pcrawl;
int k=1;
while(k<n && pcrawl!=NULL){
pcrawl = pcrawl->next;
k++;
}
if(k<n){
cout<<"not enough nodes inside\n";
return;
}
while(pcrawl->next!=NULL){
pcrawl = pcrawl->next;
prev = prev->next;
}
cout<<prev->data<<"\n";
return;
}
void print_soln(node* head){
node* pcrawl = head;
while(pcrawl != NULL){
cout<<pcrawl->data<<"->";
pcrawl = pcrawl->next;
}
cout<<"null\n";
return;
}
int main(){
node* head = NULL;
print_soln(head);
append(&head, 6);
print_soln(head);
insert_at_beginning(&head, 7);
print_soln(head);
insert_at_beginning(&head, 1);
print_soln(head);
append(&head, 4);
print_soln(head);
insert_after(&head,head->next->next, 8);
print_soln(head);
delete_key(&head, 1);
print_soln(head);
nth_from_end(&head, 3);
return 0;
}