0% found this document useful (0 votes)
8 views

Experiment 10 DSA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Experiment 10 DSA

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment 10

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:

 A computer system with a C++ compiler (Turbo C++/GCC/Dev C++)


 IDE or text editor to write and execute C++ programs

Theory:

A doubly linked list (DLL) is a data structure consisting of nodes, where each node contains
three fields:

1. data: Stores the value.


2. prev: Points to the previous node in the list.
3. next: Points to the next node in the list.

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:

1. Create Structure for Node:


o A node contains the data, prev, and next pointers.
2. Functions:
o insert: Inserts a new node with a given value at the end of the list.
o deleteNode: Deletes a node with a specific value from the list.
o search: Searches for a specific value in the list and displays its position.
o display: Displays the entire list in a forward direction.
3. Main Function:
o Create a doubly linked list object.
o Insert values into the list.
o Display the list.
o Search for specific values.
o Delete nodes with specific values and display the updated list.

Program:

#include<iostream.h>
#include<conio.h>

// Node structure for Doubly Linked List

struct Node {

int data;

Node* prev;

Node* next;

};

// Class to represent Doubly Linked List

class DoublyLinkedList {

private:

Node* head;

public:

// Constructor to initialize the list

DoublyLinkedList() {

head = NULL;

// Function to insert a node at the end of the list

void insert(int value) {

Node* newNode = new Node; // Create a new node

newNode->data = value;

newNode->prev = NULL;
newNode->next = NULL;

if (head == NULL) {

// If the list is empty, new node becomes head

head = newNode;

} else {

// Traverse to the end of the list

Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

// Insert at the end

temp->next = newNode;

newNode->prev = temp;

cout << "Inserted: " << value << endl;

// Function to delete a node from the list

void deleteNode(int value) {

if (head == NULL) {

cout << "List is empty, nothing to delete." << endl;

return;

}
Node* temp = head;

// Traverse to find the node to delete

while (temp != NULL && temp->data != value) {

temp = temp->next;

// If node not found

if (temp == NULL) {

cout << "Value " << value << " not found in the list." << endl;

return;

// Node is the head node

if (temp == head) {

head = temp->next;

// If node is not at the end, update next node's prev pointer

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;

// Free memory of the deleted node

delete temp;

cout << "Deleted: " << value << endl;

// Function to search for a value in the list

void search(int value) {

Node* temp = head;

int position = 1;

// Traverse the list to find the value

while (temp != NULL) {

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;

// Function to display the list

void display() {

if (head == NULL) {

cout << "List is empty." << endl;

return;

Node* temp = head;

cout << "Doubly Linked List: ";

while (temp != NULL) {

cout << temp->data << " ";

temp = temp->next;

cout << endl;

};

// Main function

void main() {

clrscr(); // Clear screen for Turbo C++


// Create a Doubly Linked List object

DoublyLinkedList dll;

// Inserting elements into the list

dll.insert(10);

dll.insert(20);

dll.insert(30);

dll.insert(40);

// Display the list

dll.display();

// Search for a value in the list

dll.search(20); // Searching for value 20

dll.search(50); // Searching for value 50

// Delete a node from the list

dll.deleteNode(20); // Deleting node with value 20

dll.display(); // Display the list after deletion

dll.deleteNode(50); // Attempting to delete a non-existent node

dll.display(); // Display the list after attempted deletion


getch(); // Wait for a key press before exiting

Result:

The program will successfully perform the following operations on the doubly linked list:

 Insert nodes into the list and display the list.


 Search for values and display their positions.
 Delete nodes and update the list accordingly.

Example Output:

Inserted: 10

Inserted: 20

Inserted: 30

Inserted: 40

Doubly Linked List: 10 20 30 40

Value 20 found at position 2.

Value 50 not found in the list.

Deleted: 20

Doubly Linked List: 10 30 40

Value 50 not found in the list.

Doubly Linked List: 10 30 40

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.

You might also like