-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5_linked_list2_Node_deletion.cpp
118 lines (99 loc) · 2.04 KB
/
lab5_linked_list2_Node_deletion.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
// There are 5 kind of deletetion in linked list.
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int info;
Node *link;
};
Node *head = NULL, *tem;
void allocate(int data) {
Node *ptr = new Node();
ptr->info = data;
ptr->link = NULL;
if (head == NULL) {
head = ptr;
} else {
tem->link = ptr;
}
tem = ptr;
}
void traverse() {
Node *temp = head;
while(temp != NULL) {
cout << temp->info << " ";
temp = temp->link;
}
cout << endl;
}
void delete_first() {
Node *temp = head->link;
head = temp;
}
void delete_last() {
Node *ptr = head;
Node *prev;
while (ptr->link != NULL) {
prev = ptr;
ptr = ptr->link;
}
prev->link = NULL;
tem = prev;
}
void delete_centre(int srch) {
Node *ptr = head;
Node *prev;
while (ptr->info != srch) {
prev = ptr;
ptr = ptr->link;
}
prev->link = ptr->link;
}
void delete_before(int srch) {
Node *ptr = head;
Node *prev;
while (ptr->info != srch) {
prev = ptr;
ptr = ptr->link;
}
delete_centre(prev->info);
}
void delete_after(int srch) {
Node *ptr = head;
while (ptr->info != srch) {
ptr = ptr->link;
}
ptr = ptr->link;
delete_centre(ptr->info);
}
int main() {
allocate(10);
allocate(20);
allocate(30);
allocate(40);
allocate(50);
allocate(60);
allocate(70);
allocate(80);
allocate(90);
allocate(100);
allocate(110);
allocate(120);
traverse();
cout << endl << "After deleting first Node: ";
delete_first();
traverse();
cout << endl << "After deleting last Node: ";
delete_last();
traverse();
cout << endl << "After deleting a given Node: ";
delete_centre(60);
traverse();
cout << endl << "After deleting previous Node of a given node: ";
delete_before(50);
traverse();
cout << endl << "After deleting next Node of a given node: ";
delete_after(90);
traverse();
return 0;
}