0% found this document useful (0 votes)
25 views6 pages

Doubly Linked List

A doubly linked list is a data structure where each node contains links to both its previous and next nodes, allowing for traversal in both directions. Key operations include insertion (at the beginning, end, or specific position), deletion (from the beginning, end, or specific position), and traversal (forward and backward). Each node consists of three fields: a link to the previous node, a link to the next node, and the data value.

Uploaded by

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

Doubly Linked List

A doubly linked list is a data structure where each node contains links to both its previous and next nodes, allowing for traversal in both directions. Key operations include insertion (at the beginning, end, or specific position), deletion (from the beginning, end, or specific position), and traversal (forward and backward). Each node consists of three fields: a link to the previous node, a link to the next node, and the data value.

Uploaded by

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

Doubly Linked List

 Double linked list is a sequence of elements in which every element has links to its previous
element and next element in the sequence
 We can traverse forward by using the next field and can traverse backward by using the
previous field
 Every node in a double linked list contains three fields

1. ‘link1’ field is used to store the address of the previous node in the sequence
2. ‘link2’ field is used to store the address of the next node in the sequence
3. ‘data’ field is used to store the actual value of that node

 In double linked list, first node must be always pointed by head


 Always the pervious field of the first node must be NULL
 Always the next field of the last node must be NULL

Operations on Double Linked List

a) Insertion

Insertion can occur at three positions:

1. At the beginning
2. At the end
3. At a specific position

b) Deletion

Nodes can be deleted from:

1. Beginning
2. End
3. Specific Position

c) Searching

To find an element in a linked list, traverse the list and compare each node’s data with the key value.
d) Traversal

Traversal means visiting each node in sequence.

a) Insertion

 Insertion at the Beginning: In this operation, a new node is added at the beginning of the list,
and it becomes the new head of the list

Step 1: Create a new node (newNode) with the given value.

Step 2: Check if the list is empty (head == NULL).

Step 3: If it is empty, set newNode->next = NULL, newNode->prev = NULL, and head =


newNode.

Step 4: If it is not empty:

Set newNode->next = head.

Set head->prev = newNode.

Update head = newNode.

 Insertion at the End: Here, a new node is appended to the end of the list, becoming the new tail
node.

Step 1: Create a newNode with the given value.

Step 2: Check if the list is empty (head == NULL).

Step 3: If empty, set head = newNode.

Step 4: If not empty:

Define a pointer temp and initialize it with head.

Traverse the list until temp->next == NULL.

Set temp->next = newNode, newNode->prev = temp.


Draw a diagram

 Insertion at a Specific Position: This operation involves adding a new node at a particular
position within the list, shifting the existing nodes accordingly.

Step 1: Create a newNode with the given value.

Step 2: Check if the list is empty (head == NULL).

Step 3: If empty, set newNode->next = NULL, newNode->prev = NULL, and head = newNode.

Step 4: If not empty:

Define a pointer temp and initialize it with head.

Move temp to the node after which newNode should be inserted.

If the node is found:

Set newNode->next = temp->next.

If temp->next! = NULL, set temp->next->prev = newNode.

Set temp->next = newNode, newNode->prev = temp.

If temp reaches the end of the list, display "Given node not found! Insertion not possible!".

. b) Deletion in a Doubly Linked List

1. Deleting from the Beginning:

o Step 1: Check if the list is empty (head == NULL).

o Step 2: If empty, display "List is empty! Deletion not possible!".

o Step 3: If not empty:

 Define a pointer temp = head.

 If there is only one node (temp->next == NULL), set head = NULL and delete
temp.
 Else:

 Move head = temp->next.

 Set head->prev = NULL.

 Delete temp.

2. Deleting from the End:

o Step 1: Check if the list is empty (head == NULL).

o Step 2: If empty, display "List is empty! Deletion not possible!".

o Step 3: If not empty:

 Define pointers temp1 = head and temp2 = NULL.

 If there is only one node, set head = NULL, delete temp1, and exit.

 Otherwise:

 Traverse to the last node (temp1->next == NULL).

 Set temp2 = temp1->prev.

 Set temp2->next = NULL.

 Delete temp1.

3. Deleting from a Specific Location:

o Step 1: Check if the list is empty (head == NULL).

o Step 2: If empty, display "List is empty! Deletion not possible!".

o Step 3: If not empty:

 Define pointers temp1 = head and temp2 = NULL.

 Traverse to the node to be deleted.


 If the node is found:

 If it is the only node, set head = NULL, delete temp1, and exit.

 If it is the first node, move head = temp1->next, set head->prev =


NULL, delete temp1.

 If it is the last node, set temp1->prev->next = NULL, delete temp1.

 Otherwise, update pointers:

 temp1->prev->next = temp1->next

 temp1->next->prev = temp1->prev

 Delete temp1

 If the node is not found, display "Given node not found! Deletion not
possible!".

c) Traversal in a Doubly Linked List

1. Forward Traversal:

o Step 1: Check if the list is empty (head == NULL).

o Step 2: If empty, display "List is empty!".

o Step 3: If not empty:

 Define a pointer temp = head.

 Traverse the list, displaying temp->data until temp == NULL.

2. Backward Traversal:

o Step 1: Traverse to the last node (tail).

o Step 2: Print the elements in reverse order

(temp = tail;

while (temp! = NULL)

{print(temp->data); temp = temp->prev;

}).

You might also like