Experiment 10 DSA
Experiment 10 DSA
Objective:
To write a C++ program that implements a doubly linked list (DLL) with the functionalities
of inserting, deleting, searching, and displaying elements. The program also handles various
cases, such as when the list is empty, when a node is at the beginning or end, and when an
attempt is made to delete a non-existent node.
Apparatus:
Theory:
A doubly linked list (DLL) is a data structure consisting of nodes, where each node contains
three fields:
Unlike singly linked lists, DLLs allow traversal in both directions (forward and backward),
making them more versatile but slightly more complex in terms of memory usage. This
program allows for the insertion of nodes at the end of the list, searching for a value in the
list, deletion of a node, and displaying the contents of the list.
Procedure:
Program:
#include<iostream.h>
#include<conio.h>
struct Node {
int data;
Node* prev;
Node* next;
};
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() {
head = NULL;
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
if (head == NULL) {
return;
}
Node* temp = head;
temp = temp->next;
if (temp == NULL) {
cout << "Value " << value << " not found in the list." << endl;
return;
if (temp == head) {
head = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
// If node is not at the beginning, update previous node's next pointer
if (temp->prev != NULL) {
temp->prev->next = temp->next;
delete temp;
int position = 1;
if (temp->data == value) {
cout << "Value " << value << " found at position " << position << "." << endl;
return;
temp = temp->next;
position++;
}
cout << "Value " << value << " not found in the list." << endl;
void display() {
if (head == NULL) {
return;
temp = temp->next;
};
// Main function
void main() {
DoublyLinkedList dll;
dll.insert(10);
dll.insert(20);
dll.insert(30);
dll.insert(40);
dll.display();
Result:
The program will successfully perform the following operations on the doubly linked list:
Example Output:
Inserted: 10
Inserted: 20
Inserted: 30
Inserted: 40
Deleted: 20
Precautions:
1. Ensure proper memory management to avoid memory leaks (especially when deleting
nodes).
2. Check for edge cases like deleting from an empty list or attempting to delete a non-
existent node.
3. Make sure the prev and next pointers are correctly updated when inserting or
deleting nodes.
4. Validate user inputs and handle any possible null pointer dereferencing.