1
+ /* *
2
+ * @file double_linked_list.cpp
3
+ * @brief Implementation of a doubly linked list with basic operations.
4
+ *
5
+ * This program implements a doubly linked list with operations such as
6
+ * insertion, deletion, search, forward traversal, and reverse traversal.
7
+ */
8
+
1
9
#include < cstdio>
2
10
#include < cstdlib>
3
11
#include < iostream>
4
12
13
+ /* *
14
+ * @brief Node structure for the doubly linked list.
15
+ *
16
+ * Each node contains an integer value, a pointer to the previous node,
17
+ * and a pointer to the next node.
18
+ */
5
19
struct node {
6
- int val;
7
- node *prev;
8
- node *next;
9
- } * start;
20
+ int val; // /< Value stored in the node.
21
+ node *prev; // /< Pointer to the previous node.
22
+ node *next; // /< Pointer to the next node.
23
+ } *start; // /< Pointer to the start of the doubly linked list.
10
24
25
+ /* *
26
+ * @brief Class representing a doubly linked list.
27
+ *
28
+ * This class provides methods to insert, remove, search, display the list
29
+ * in forward and reverse order.
30
+ */
11
31
class double_linked_list {
12
32
public:
33
+ /* *
34
+ * @brief Constructor to initialize the doubly linked list.
35
+ */
13
36
double_linked_list () { start = NULL ; }
37
+
38
+ /* *
39
+ * @brief Inserts an element at the end of the doubly linked list.
40
+ *
41
+ * @param x The value to be inserted.
42
+ *
43
+ * @note Time Complexity: O(n), where n is the number of nodes in the list.
44
+ * @note Space Complexity: O(1), only extra space for a new node.
45
+ */
14
46
void insert (int x);
47
+
48
+ /* *
49
+ * @brief Removes the first occurrence of a given value from the list.
50
+ *
51
+ * @param x The value to be removed.
52
+ *
53
+ * @note Time Complexity: O(n), where n is the number of nodes in the list.
54
+ * @note Space Complexity: O(1), no extra space used aside from traversal.
55
+ */
15
56
void remove (int x);
57
+
58
+ /* *
59
+ * @brief Searches for a value in the doubly linked list.
60
+ *
61
+ * @param x The value to be searched.
62
+ *
63
+ * @note Time Complexity: O(n), where n is the number of nodes in the list.
64
+ * @note Space Complexity: O(1), no extra space used aside from traversal.
65
+ */
16
66
void search (int x);
67
+
68
+ /* *
69
+ * @brief Displays the list in forward order.
70
+ *
71
+ * @note Time Complexity: O(n), where n is the number of nodes in the list.
72
+ * @note Space Complexity: O(1), no extra space used.
73
+ */
17
74
void show ();
75
+
76
+ /* *
77
+ * @brief Displays the list in reverse order.
78
+ *
79
+ * @note Time Complexity: O(n), where n is the number of nodes in the list.
80
+ * @note Space Complexity: O(1), no extra space used.
81
+ */
18
82
void reverseShow ();
19
83
};
20
84
85
+ /* *
86
+ * @brief Inserts an element at the end of the doubly linked list.
87
+ *
88
+ * @param x The value to be inserted.
89
+ */
21
90
void double_linked_list::insert (int x) {
22
91
node *t = start;
23
92
if (start != NULL ) {
@@ -38,6 +107,11 @@ void double_linked_list::insert(int x) {
38
107
}
39
108
}
40
109
110
+ /* *
111
+ * @brief Removes the first occurrence of a given value from the list.
112
+ *
113
+ * @param x The value to be removed.
114
+ */
41
115
void double_linked_list::remove (int x) {
42
116
node *t = start;
43
117
while (t != NULL && t->val != x) {
@@ -46,22 +120,27 @@ void double_linked_list::remove(int x) {
46
120
if (t == NULL ) {
47
121
return ;
48
122
}
49
- if (t->prev == NULL ) {
123
+ if (t->prev == NULL ) { // Removing the first node
50
124
if (t->next == NULL ) {
51
125
start = NULL ;
52
126
} else {
53
127
start = t->next ;
54
128
start->prev = NULL ;
55
129
}
56
- } else if (t->next == NULL ) {
130
+ } else if (t->next == NULL ) { // Removing the last node
57
131
t->prev ->next = NULL ;
58
- } else {
132
+ } else { // Removing a middle node
59
133
t->prev ->next = t->next ;
60
134
t->next ->prev = t->prev ;
61
135
}
62
136
delete t;
63
137
}
64
138
139
+ /* *
140
+ * @brief Searches for a value in the doubly linked list.
141
+ *
142
+ * @param x The value to be searched.
143
+ */
65
144
void double_linked_list::search (int x) {
66
145
node *t = start;
67
146
int found = 0 ;
@@ -78,6 +157,9 @@ void double_linked_list::search(int x) {
78
157
}
79
158
}
80
159
160
+ /* *
161
+ * @brief Displays the list in forward order.
162
+ */
81
163
void double_linked_list::show () {
82
164
node *t = start;
83
165
while (t != NULL ) {
@@ -86,6 +168,9 @@ void double_linked_list::show() {
86
168
}
87
169
}
88
170
171
+ /* *
172
+ * @brief Displays the list in reverse order.
173
+ */
89
174
void double_linked_list::reverseShow () {
90
175
node *t = start;
91
176
while (t != NULL && t->next != NULL ) {
@@ -97,6 +182,13 @@ void double_linked_list::reverseShow() {
97
182
}
98
183
}
99
184
185
+ /* *
186
+ * @brief Main function to test the doubly linked list operations.
187
+ *
188
+ * The function provides a menu to insert, delete, search, and display the list.
189
+ *
190
+ * @return 0 on successful execution.
191
+ */
100
192
int main () {
101
193
int choice, x;
102
194
double_linked_list ob;
@@ -106,21 +198,21 @@ int main() {
106
198
std::cout << " \n 3. Search" ;
107
199
std::cout << " \n 4. Forward print" ;
108
200
std::cout << " \n 5. Reverse print" ;
109
- std::cout << " \n\n Enter you choice : " ;
201
+ std::cout << " \n\n Enter your choice: " ;
110
202
std::cin >> choice;
111
203
switch (choice) {
112
204
case 1 :
113
- std::cout << " \n Enter the element to be inserted : " ;
205
+ std::cout << " \n Enter the element to be inserted: " ;
114
206
std::cin >> x;
115
207
ob.insert (x);
116
208
break ;
117
209
case 2 :
118
- std::cout << " \n Enter the element to be removed : " ;
210
+ std::cout << " \n Enter the element to be removed: " ;
119
211
std::cin >> x;
120
212
ob.remove (x);
121
213
break ;
122
214
case 3 :
123
- std::cout << " \n Enter the element to be searched : " ;
215
+ std::cout << " \n Enter the element to be searched: " ;
124
216
std::cin >> x;
125
217
ob.search (x);
126
218
break ;
0 commit comments