Skip to content

Commit 26d2d3f

Browse files
authored
Documentation Update doubly_linked_list.cpp
1 parent d438f0f commit 26d2d3f

File tree

1 file changed

+103
-11
lines changed

1 file changed

+103
-11
lines changed

data_structures/doubly_linked_list.cpp

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,92 @@
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+
19
#include <cstdio>
210
#include <cstdlib>
311
#include <iostream>
412

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+
*/
519
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.
1024

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+
*/
1131
class double_linked_list {
1232
public:
33+
/**
34+
* @brief Constructor to initialize the doubly linked list.
35+
*/
1336
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+
*/
1446
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+
*/
1556
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+
*/
1666
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+
*/
1774
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+
*/
1882
void reverseShow();
1983
};
2084

85+
/**
86+
* @brief Inserts an element at the end of the doubly linked list.
87+
*
88+
* @param x The value to be inserted.
89+
*/
2190
void double_linked_list::insert(int x) {
2291
node *t = start;
2392
if (start != NULL) {
@@ -38,6 +107,11 @@ void double_linked_list::insert(int x) {
38107
}
39108
}
40109

110+
/**
111+
* @brief Removes the first occurrence of a given value from the list.
112+
*
113+
* @param x The value to be removed.
114+
*/
41115
void double_linked_list::remove(int x) {
42116
node *t = start;
43117
while (t != NULL && t->val != x) {
@@ -46,22 +120,27 @@ void double_linked_list::remove(int x) {
46120
if (t == NULL) {
47121
return;
48122
}
49-
if (t->prev == NULL) {
123+
if (t->prev == NULL) { // Removing the first node
50124
if (t->next == NULL) {
51125
start = NULL;
52126
} else {
53127
start = t->next;
54128
start->prev = NULL;
55129
}
56-
} else if (t->next == NULL) {
130+
} else if (t->next == NULL) { // Removing the last node
57131
t->prev->next = NULL;
58-
} else {
132+
} else { // Removing a middle node
59133
t->prev->next = t->next;
60134
t->next->prev = t->prev;
61135
}
62136
delete t;
63137
}
64138

139+
/**
140+
* @brief Searches for a value in the doubly linked list.
141+
*
142+
* @param x The value to be searched.
143+
*/
65144
void double_linked_list::search(int x) {
66145
node *t = start;
67146
int found = 0;
@@ -78,6 +157,9 @@ void double_linked_list::search(int x) {
78157
}
79158
}
80159

160+
/**
161+
* @brief Displays the list in forward order.
162+
*/
81163
void double_linked_list::show() {
82164
node *t = start;
83165
while (t != NULL) {
@@ -86,6 +168,9 @@ void double_linked_list::show() {
86168
}
87169
}
88170

171+
/**
172+
* @brief Displays the list in reverse order.
173+
*/
89174
void double_linked_list::reverseShow() {
90175
node *t = start;
91176
while (t != NULL && t->next != NULL) {
@@ -97,6 +182,13 @@ void double_linked_list::reverseShow() {
97182
}
98183
}
99184

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+
*/
100192
int main() {
101193
int choice, x;
102194
double_linked_list ob;
@@ -106,21 +198,21 @@ int main() {
106198
std::cout << "\n3. Search";
107199
std::cout << "\n4. Forward print";
108200
std::cout << "\n5. Reverse print";
109-
std::cout << "\n\nEnter you choice : ";
201+
std::cout << "\n\nEnter your choice: ";
110202
std::cin >> choice;
111203
switch (choice) {
112204
case 1:
113-
std::cout << "\nEnter the element to be inserted : ";
205+
std::cout << "\nEnter the element to be inserted: ";
114206
std::cin >> x;
115207
ob.insert(x);
116208
break;
117209
case 2:
118-
std::cout << "\nEnter the element to be removed : ";
210+
std::cout << "\nEnter the element to be removed: ";
119211
std::cin >> x;
120212
ob.remove(x);
121213
break;
122214
case 3:
123-
std::cout << "\nEnter the element to be searched : ";
215+
std::cout << "\nEnter the element to be searched: ";
124216
std::cin >> x;
125217
ob.search(x);
126218
break;

0 commit comments

Comments
 (0)