Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views42 pages

Doubly Linked List - U

A doubly linked list (DLL) is a complex data structure that contains pointers to both the next and previous nodes, allowing for bidirectional traversal. The document outlines basic operations on DLLs, including traversal, searching, insertion, and deletion, along with algorithms for each operation. Additionally, it introduces circular doubly linked lists, which eliminate NULL pointers at both ends, enhancing efficiency in certain applications.

Uploaded by

sardhiksai
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)
11 views42 pages

Doubly Linked List - U

A doubly linked list (DLL) is a complex data structure that contains pointers to both the next and previous nodes, allowing for bidirectional traversal. The document outlines basic operations on DLLs, including traversal, searching, insertion, and deletion, along with algorithms for each operation. Additionally, it introduces circular doubly linked lists, which eliminate NULL pointers at both ends, enhancing efficiency in certain applications.

Uploaded by

sardhiksai
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/ 42

Data Structures

Doubly Linked List (DLL)


List Overview
■ DOUBLY LINKED LIST:
■ Introduction

■ BASIC OPERATIONS ON A DOUBLY LINKED LIST:

■ Traverse
■ Search

■ Insert

■ Delete

■ APPLICATIONS OF DOUBLY LINKED LIST


Doubly Linked List
▪ A doubly linked list or a two way linked list is a more complex
type of linked list which contains a pointer to the next as well
as previous node in the sequence. Therefore, it consists of
three parts and not just two. The three parts are data, a
pointer to the next node and a pointer to the previous node
START

X 4 X
1 1 2 3

• In C language, the structure of a doubly linked list is given as,


struct node
{ struct node *prev; int
data;
struct node *next;
};
• The prev field of the first node and the next field of the last
node will contain NULL. The prev field is used to store the
address of the preceding node. This would enable to traverse
the list in the backward direction as well.
Double Linked List: Basic

Operations

TRAVERSE : A GIVEN LIST


Algorithm to traverse through a Double Linked
List

Step 1: IF HEAD == NULL, then


Write “UNDERFLOW”
Go to Step 6
[END OF IF]

Step 2: SET PTR = HEAD


Step 3: Repeat Steps 4 and 5 while PTR != NULL Step
3: Apply PROCESS to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT

int traverse() {
struct node *ptr;
if(head == NULL)
{
printf("\nEmpty List\n");
} else { ptr =
head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr>next;
}
}
}
Double Linked List: Basic

Operations

SEARCH : A GIVEN LIST

Algorithm to search through a Double Linked


List

Step 1: IF HEAD == NULL, then


Write “UNDERFLOW”
Go to Step 8
[END OF IF]

Step 2: SET PTR = HEAD


Step 3: SET I = 0
Step 4: Repeat Steps 5 to 7 while PTR != NULL
Step 5: IF PTR->DATA == ITEM
RETURN I [END OF IF]
Step 6: SET I = I+1
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: EXIT

void search()
{
struct node *ptr; int item,i=0,flag; ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
{ printf("\nEnter item which you want to search?\n");
scanf("%d",&item); while (ptr!=NULL)
{ if(ptr->data == item)
{ printf("\nitem found at
location %d ",i+1); flag=0; break;
} else {
flag=1;
}
i++; ptr = ptr -> next;
}
if(flag==1) printf("\nItem not found\n");
}
}

Doubly Linked List: Basic Operations

INSERT : AT FRONT OF THE LIST


Algorithm to insert a new node in the beginning of
the doubly linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 8
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->PREV = NULL
Step 6: SET New_Node->Next = START
Step 7: SET START = New_Node
Step 8: EXIT
Doubly Linked List: Basic Operations

INSERT : AT LAST OF THE LIST


Algorithm to insert a new node at the end of the doubly
linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 11 [END
OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node Step 10:
New_Node->PREV = PTR
Doubly Linked List: Basic Operations

INSERT : AFTER A GIVEN NODE OF THE LIST


Algorithm to insert a new node after a node that
has value NUM

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA !=
NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT

Step 9: SET New_Node->PREV = PTR


Step 10: SET PTR->NEXT = New_Node
Step 11: EXIT
Doubly Linked List: Basic Operations

DELETE : FIRST NODE OF THE LIST


Algorithm to delete the first node from the doubly
linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: SET START->PREV = NULL
Step 5: FREE PTR
Step 6: EXIT
X 1 7 3 4 2 X

START, PTR

7 3 4 2 X
Doubly Linked List: Basic Operations

DELETE : LAST NODE OF THE LIST

Algorithm to delete the last node of the doubly


linked list
Step 1: IF START = NULL, then
Write UNDERFLOW

Go to Step 7
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR->NEXT != NULL
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET PTR->PREV->NEXT = NULL
Step 6: FREE PTR
Step 7: EXIT
Doubly Linked List: Basic Operations

DELETE : AFTER A GIVEN NODE OF THE LIST

X 1 4 7 8 X 3 9
1
START, PTR
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Doubly Circular
Step 9: EXIT

Linked List
• A circular doubly linked list or a circular two way linked list is a
more complex type of linked list which contains a pointer to
the next as well as previous node in the sequence.

• The difference between a doubly linked and a circular doubly


linked list is same as that exists between a singly linked list and
a circular linked list. The circular doubly linked list does not
contain NULL in the previous field of the first node and the next
field of the last node. Rather, the next field of the last node
stores the address of the first node of the list, i.e; START.
Similarly, the previous field of the first field stores the address

of the last node. Doubly Circular Linked


List
• Since a circular doubly linked list contains three parts in its
structure, it calls for more space per node and for more
expensive basic operations. However, it provides the ease to
manipulate the elements of the list as it maintains pointers to
nodes in both the directions . The main advantage of using a
circular doubly linked list is that it makes searches twice as
efficient.

1 1 2 3 4

START
Doubly Linked List
APPLICATIONS : DOUBLE LINKED LIST

You might also like