PRESENTATION ON
DOUBLE LINKED LIST
SUBMITTED
BY:
Nirmal pandey
CSE
CONTENTS
• Introduction to linked list
• Double linked list
• Structure of a node in double linked list
• Memory representation of double linked list
• Node creation
• Operations on linked list
INTRODUCTION TO LINKED
LIST
• A linked list is a linear collection of data elements which are
stored dynamically in memory.
• It is a data structure consisting of a collection of nodes which
together represent a sequence.
• In it’s most basic form, each node contains; data, and a
reference(in other words, a link) to the next node in the
sequence.
LINKED LIST
REPRESENTATION
• Linked list can be represented in a computer memory as a
chain of nodes, where every node points to the next node.
As per the above illustration, following are the important points
to be considered.
• Linked List contains a link element called first.
• Each link carries a data field(s) and a link field called next.
• Each link is linked with its next link using its next link.
• Last link carries a link as null to mark the end of the list.
DOUBLE LINKED LIST
• A double linked list is a complex type of linked list in which a node
contains a pointer to the previous as well as the next node in the
sequence.
• In a double linked list, a node consists of three parts; node data,
pointer to the next node in sequence(next pointer), pointer to the
previous node(previous pointer).
• A sample node in a double linked list is shown in the figure below:
STRUCTURE OF A NODE IN
DOUBLE LINKED LIST
• struct node
{
struct node *prev;
int data;
struct node *next;
}
• The prev part of the first node and the next part of the
last node will always contain null indicating to the end
in each direction.
MEMORY REPRESENTATION OF
DOUBLE LINKED LIST
• Generally, double linked list consumes more space for
every node.
• However, we can easily manipulate the elements of the
list since the list maintains pointers in both the
directions(forward and backward).
• Memory representation of double linked list is shown in
the following image in the next slide:
• In the image, the first element
of the list i.e 13 stored at address
1. The head pointer points to
the starting address 1. Since, this
is the first element being added
to the list therefore the prev of
the list contains NULL(-1).
• The next node of the list resides
at address 4 therefore the first
node contains 4 in its next
pointer.
• We can transverse the list in this
way until we find any node
containing NULL(-1) in it’s part.
NODE CREATION OF DOUBLE
LINKED LIST
• struct node
• {
• struct node *prev; //pointer variable pointing to the
previous node.
• int data; //data field of the node
• struct node *next; //pointer variable pointing to the
next node
• };
• struct node *head; //structure variable
Operations on double
linked list
The basic operations performed on a double linked list
are:
1. Traversal
2. Searching
3. Insertion
4. Deletion
TRAVERSING
Traversing is the most common operation in case of each
data structure. For this purpose, copy the head pointer to
any of the temporary pointer (let’s say ptr).
then, traverse through the list by using while loop. Keep
shifting value of pointer variables ptr until we find the last
node. The last node contains NULL in it’s next part.
ALGORITHM FOR TRAVERSING
Step1: If Head==Null
write”underflow”
GOTO step 6
[End of If]
Step 2: Set prt=head
Step 3: Repeat 4 and 5 while ptr!=Null
Step 4: write ptr->data
Step 5: Exit
Example:
While(ptr!=Null)
{
printf(“%dn” ptr->data);
ptr=ptr->next;
}
SEARCHING
• Step 1: IF HEAD == NULL
• WRITE "UNDERFLOW"
GOTO STEP 8
[END OF IF]
• Step 2: Set PTR = HEAD
• Step 3: Set i = 0
• Step 4: Repeat step 5 to 7 while PTR != NULL
• Step 5: IF PTR → data = item
• return i
[END OF IF]
• Step 6: i = i + 1
• Step 7: PTR = PTR → next
• Step 8: Exit
INSERTION IN A DOUBLE
LINKED LIST
In a double linked list, the insertion operation can be
performed in three ways as follows...
• Inserting At Beginning of the list
• Inserting At End of the list
• Inserting At Specific location in the list
INSERTING AT BEGINNING OF
THE LIST
ALGORITHM:
STEP1: Set space for new node
STEP2: Assign value to the item field of new node.
STEP3: Set the next field of the new node to point to the start of the list
STEP4: Set the prev field of the new node to point to the Null
STEP5: Change the head pointer to point to the new node.
INSERTING AT END OF THE
LIST
ALGORITHM:
STEP1: Set space for new node
STEP2: Assign value to the item field of new node.
STEP3: Set the next field of the new node to point to the NULL.
STEP4: Set the prev field of the new node to point to the last item in the list.
INSERTING AT SPECIFIC
LOCATION IN THE LIST
ALGORITHM:
STEP1: Set space for new node
STEP2: Assign value to the item field of new node.
STEP3: Traverse the list by using the pointer temp to skip the required number of
nodes in order to reach the specified node.
STEP4: The temp would point to the specified node at the end of the for loop.
The new node needs to be inserted after this node.
STEP5: Make the prev of the new node ptr point to temp.
STEP6: Make the next pointer of temp point to the new node ptr.
STEP7: Make the previous pointer of the next node of temp point to the new node.
INSERTING AT SPECIFIC
LOCATION IN THE LIST
DELETION IN A DOUBLE
LINKED LIST
In a double linked list, the deletion operation can be
performed in three ways as follows...
• Deletion At Beginning of the list
• Deletion At End of the list
• Deletion At Specific location in the list
DELETING FROM THE
BEGINNING OF THE LIST
ALGORITHM:
STEP1: Copy the head pointer to pointer ptr and shift the head pointer to its next.
STEP2: Make the prev of this new node point to Null.
STEP3: Free the pointer ptr by using free function.
DELETING FROM THE END OF
THE LIST
ALGORITHM:
STEP1: If the list is already empty then the condition head == NULL will become
true and therefore the operation can not be carried on.
STEP2: If there is only one node in the list then the condition head → next ==
NULL become true. In this case, we just need to assign the head of the list to
NULL and free head in order to completely delete the list.
STEP3: Otherwise, just traverse the list to reach the last node of the list. This
will be done by using the following statements.
STEP4: The temp would point to the last node of the list at the end of the for
loop. Just make the next pointer of the previous node of temp to NULL.
STEP5: Free the pointer as this the node which is to be deleted.
DELETING FROM THE END OF
THE LIST
DELETING FROM SPECIFIC
LOCATION IN THE LIST
ALGORITHM:
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET TEMP = HEAD
Step 3: Repeat Step 4 while TEMP -> DATA != ITEM
Step 4: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 5: SET PTR = TEMP -> NEXT
Step 6: SET TEMP -> NEXT = PTR -> NEXT
Step 7: SET PTR -> NEXT -> PREV = TEMP
Step 8: FREE PTR
Step 9: EXIT
DELETING FROM SPECIFIC
LOCATION IN THE LIST
ANY QUESTION S REGARDING
THE DOUBLE LINKED LIST ?
Thank you !!

Dounly linked list

  • 1.
    PRESENTATION ON DOUBLE LINKEDLIST SUBMITTED BY: Nirmal pandey CSE
  • 2.
    CONTENTS • Introduction tolinked list • Double linked list • Structure of a node in double linked list • Memory representation of double linked list • Node creation • Operations on linked list
  • 3.
    INTRODUCTION TO LINKED LIST •A linked list is a linear collection of data elements which are stored dynamically in memory. • It is a data structure consisting of a collection of nodes which together represent a sequence. • In it’s most basic form, each node contains; data, and a reference(in other words, a link) to the next node in the sequence.
  • 4.
    LINKED LIST REPRESENTATION • Linkedlist can be represented in a computer memory as a chain of nodes, where every node points to the next node. As per the above illustration, following are the important points to be considered. • Linked List contains a link element called first. • Each link carries a data field(s) and a link field called next. • Each link is linked with its next link using its next link. • Last link carries a link as null to mark the end of the list.
  • 5.
    DOUBLE LINKED LIST •A double linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. • In a double linked list, a node consists of three parts; node data, pointer to the next node in sequence(next pointer), pointer to the previous node(previous pointer). • A sample node in a double linked list is shown in the figure below:
  • 6.
    STRUCTURE OF ANODE IN DOUBLE LINKED LIST • struct node { struct node *prev; int data; struct node *next; } • The prev part of the first node and the next part of the last node will always contain null indicating to the end in each direction.
  • 7.
    MEMORY REPRESENTATION OF DOUBLELINKED LIST • Generally, double linked list consumes more space for every node. • However, we can easily manipulate the elements of the list since the list maintains pointers in both the directions(forward and backward). • Memory representation of double linked list is shown in the following image in the next slide:
  • 9.
    • In theimage, the first element of the list i.e 13 stored at address 1. The head pointer points to the starting address 1. Since, this is the first element being added to the list therefore the prev of the list contains NULL(-1). • The next node of the list resides at address 4 therefore the first node contains 4 in its next pointer. • We can transverse the list in this way until we find any node containing NULL(-1) in it’s part.
  • 10.
    NODE CREATION OFDOUBLE LINKED LIST • struct node • { • struct node *prev; //pointer variable pointing to the previous node. • int data; //data field of the node • struct node *next; //pointer variable pointing to the next node • }; • struct node *head; //structure variable
  • 11.
    Operations on double linkedlist The basic operations performed on a double linked list are: 1. Traversal 2. Searching 3. Insertion 4. Deletion
  • 12.
    TRAVERSING Traversing is themost common operation in case of each data structure. For this purpose, copy the head pointer to any of the temporary pointer (let’s say ptr). then, traverse through the list by using while loop. Keep shifting value of pointer variables ptr until we find the last node. The last node contains NULL in it’s next part.
  • 13.
    ALGORITHM FOR TRAVERSING Step1:If Head==Null write”underflow” GOTO step 6 [End of If] Step 2: Set prt=head Step 3: Repeat 4 and 5 while ptr!=Null Step 4: write ptr->data Step 5: Exit Example: While(ptr!=Null) { printf(“%dn” ptr->data); ptr=ptr->next; }
  • 14.
    SEARCHING • Step 1:IF HEAD == NULL • WRITE "UNDERFLOW" GOTO STEP 8 [END OF IF] • Step 2: Set PTR = HEAD • Step 3: Set i = 0 • Step 4: Repeat step 5 to 7 while PTR != NULL • Step 5: IF PTR → data = item • return i [END OF IF] • Step 6: i = i + 1 • Step 7: PTR = PTR → next • Step 8: Exit
  • 15.
    INSERTION IN ADOUBLE LINKED LIST In a double linked list, the insertion operation can be performed in three ways as follows... • Inserting At Beginning of the list • Inserting At End of the list • Inserting At Specific location in the list
  • 16.
    INSERTING AT BEGINNINGOF THE LIST ALGORITHM: STEP1: Set space for new node STEP2: Assign value to the item field of new node. STEP3: Set the next field of the new node to point to the start of the list STEP4: Set the prev field of the new node to point to the Null STEP5: Change the head pointer to point to the new node.
  • 17.
    INSERTING AT ENDOF THE LIST ALGORITHM: STEP1: Set space for new node STEP2: Assign value to the item field of new node. STEP3: Set the next field of the new node to point to the NULL. STEP4: Set the prev field of the new node to point to the last item in the list.
  • 18.
    INSERTING AT SPECIFIC LOCATIONIN THE LIST ALGORITHM: STEP1: Set space for new node STEP2: Assign value to the item field of new node. STEP3: Traverse the list by using the pointer temp to skip the required number of nodes in order to reach the specified node. STEP4: The temp would point to the specified node at the end of the for loop. The new node needs to be inserted after this node. STEP5: Make the prev of the new node ptr point to temp. STEP6: Make the next pointer of temp point to the new node ptr. STEP7: Make the previous pointer of the next node of temp point to the new node.
  • 19.
  • 20.
    DELETION IN ADOUBLE LINKED LIST In a double linked list, the deletion operation can be performed in three ways as follows... • Deletion At Beginning of the list • Deletion At End of the list • Deletion At Specific location in the list
  • 21.
    DELETING FROM THE BEGINNINGOF THE LIST ALGORITHM: STEP1: Copy the head pointer to pointer ptr and shift the head pointer to its next. STEP2: Make the prev of this new node point to Null. STEP3: Free the pointer ptr by using free function.
  • 22.
    DELETING FROM THEEND OF THE LIST ALGORITHM: STEP1: If the list is already empty then the condition head == NULL will become true and therefore the operation can not be carried on. STEP2: If there is only one node in the list then the condition head → next == NULL become true. In this case, we just need to assign the head of the list to NULL and free head in order to completely delete the list. STEP3: Otherwise, just traverse the list to reach the last node of the list. This will be done by using the following statements. STEP4: The temp would point to the last node of the list at the end of the for loop. Just make the next pointer of the previous node of temp to NULL. STEP5: Free the pointer as this the node which is to be deleted.
  • 23.
    DELETING FROM THEEND OF THE LIST
  • 24.
    DELETING FROM SPECIFIC LOCATIONIN THE LIST ALGORITHM: Step 1: IF HEAD = NULL Write UNDERFLOW Go to Step 9 [END OF IF] Step 2: SET TEMP = HEAD Step 3: Repeat Step 4 while TEMP -> DATA != ITEM Step 4: SET TEMP = TEMP -> NEXT [END OF LOOP] Step 5: SET PTR = TEMP -> NEXT Step 6: SET TEMP -> NEXT = PTR -> NEXT Step 7: SET PTR -> NEXT -> PREV = TEMP Step 8: FREE PTR Step 9: EXIT
  • 25.
  • 26.
    ANY QUESTION SREGARDING THE DOUBLE LINKED LIST ?
  • 27.