-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteNOde.cpp
78 lines (71 loc) · 1.5 KB
/
deleteNOde.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
#include <iostream>
using namespace std ;
int sie ; // global variable
class node {
public:
int data;
node *next ;
};
node *first , *last ;
void create(int *p , int size ) {
node *ptr = new node ;
first = ptr ;
ptr->data = p[0] ;
ptr->next = nullptr ;
last = ptr ;
for(int i = 1 ; i<size ; i++){
node *ptr = new node ;
ptr->data = *(p+i);
last->next = ptr ;
last = ptr ;
last->next = nullptr ;
}
sie = size ;}
void display(node *ptr){
while (ptr!=nullptr)
{
cout<<ptr->data<<" ";
ptr=ptr->next ;
/* code */
}
cout<<endl;
}
void deleteNode(int pos){
if(pos==1){
node *ptr = first ;
first = first->next;
cout<<"Task completed : node with data "<<ptr->data<<" deleted successfully "<<endl;
delete ptr ;
sie-- ;
}
else{
node *ptr , *qtr ;
ptr = nullptr ;
qtr = first ;
if(pos>sie){
cout<<"Invalid index"<<endl;
}
else {
for(int i = 0 ;i<pos-1 ; i++){
ptr = qtr ;
qtr = qtr->next ;
}
ptr->next = qtr->next ;
cout<<"Task completed : node with data "<<qtr->data<<" deleted successfully "<<endl;
delete qtr;
sie-- ;
}
}
}
int main(){
int a[5] = {40 , 400 , 4 , 14 , 44 };
create(a,5);
display(first);
deleteNode(3);
display(first);
deleteNode(1);
display(first);
deleteNode(3);
display(first);
return 0 ;
}