DOUBLY LINKED LIST
CREATED BY:
1507011- TAHMINA RAHMAN
1507012- SAMIUL AZIM
1507013- FABLIHA HAQUE
1507014- SALIM SHADMAN ANKUR
1507015- SAIFUR RAHMAN
TOPICS:
• linked lists
• doubly linked list
• Creation and traversing of doubly linked list
• Insertion and deletion on doubly linked list
• complexity of insertion and deletion
• Advantages and disadvantages of doubly linked list
LINKED LIST
 Like arrays, Linked List is a linear data structure
 Unlike arrays, linked list elements are not stored at contiguous
location
 the elements are linked using pointers
 does not have any index
ADDRESSES FOR LINKED LIST AND ARRAY
Linked List Arrray
WHY WE USE LINKED LIST
Arrays can be used to store linear data of similar
types, but arrays have following limitations..
1) The size of the arrays is fixed .
2) Inserting a new element in an array of elements is expensive, because room
has to be created for the new elements and to create room existing elements
have to shifted.
DOUBLY LINKED LIST
• A Doubly Linked List (DLL) contains an extra pointer, typically called previous
pointer, together with next pointer and data which are there in singly linked
list.
• Every nodes in the doubly linked list has three fields:
1. Left Pointer (prev)
2. Right Pointer (next)
3. Data (info)
DOUBLY LINKED LIST
• Prev - address of the previous node
• Next - address of the next node
• Info - information of the node
• head - address of the first node
Prev Info Next
node
CREATION OF DOUBLY LINKED LIST
Steps-
1: Head := NULL (as global)
2: New Node (Ptr)
// set data and prev , next to NULL
3: If Head = NULL
 Head := ptr
 temp := ptr
4. If not
 temp → next := ptr
 Ptr → prev := temp
 temp := ptr
CREATION OF DOUBLY LINKED LIST
NULL data1 NULL
0x80
0x80 data2 NULLNULL data1 0x81
0x810x80
0x80 data2 0x81NULL data1 0x81 0x81 data3 NULL
0x80 0x81 0x82
No element inserted
Third element inserted:
Second element inserted:
First element inserted:
Empty list
Head
temp
Head
temp
Head
temp
TRAVERSING
0x80 9 0x82NULL 8 0x81 0x81 10 NULL
Head Srt
Steps:
Set srt := head
while ( srt->next != NULL)
print srt->info // 8
srt := srt->next
if (srt->next == NULL)
print srt->info
end of traversing
0x80 0x81 0x82
False
Not Executed
Output:
8
Srt
0x80
0x81
Srt
TRAVERSING
0x80 9 0x82NULL 8 0x81 0x81 10 NULL
Head Srt
Steps:
Set srt := head
while ( srt->next != NULL)
print srt->info // 9
srt := srt->next
if (srt->next = NULL)
print srt->info
end of traversing
0x80 0x81 0x82
True
Output:
8
9
10
Srt
0x80
0x81
0x82
Srt
OPERATIONS ON DOUBLY LINKED LIST
Insertion
• Insert an element at first
• Insert an element at last
• Insert before a given element
• Insert after a given element
Deletion
• Delete first element
• Delete last element
• Delete a given element
• Delete before a given element
• Delete after a given element
INSERT AN ELEMENT AT FIRST
prev info NULLprev info nextNULL info next
Head
Steps-
1. New node (Ptr)
prev info next
Ptr
INSERT AN ELEMENT AT FIRST
prev info NULLprev info nextinfo next
Head
data
Steps-
1. New node (Ptr)
2. Ptr → info := data
3. Ptr → prev := NULL
4. Ptr → next := NULL
Ptr
NULL
NULL
NULL
INSERT AN ELEMENT AT FIRST
prev info NULLprev info nextinfo next
Head
NULL data
Steps-
1. New node (Ptr)
2. Ptr → Info := data
3. Ptr → prev := NULL
4. Ptr → next := NULL
5. Head → prev := ptr
6. Ptr → next := head
7. Head := Ptr
Ptr
NULL
NULL
Head
EXAMPLE OF FIRST INSERTION:
This is a doubly linked list ….
0x81 8 XX 1 0x81 0x80 5 0x82
Inserting 45 at the first of the list
0x820x810x80
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
X 45 X
0x90
0X900X80
INSERT AN ELEMENT AT LAST
prev info NULLprev info nextNULL info next
prev info next
srt
Ptr
Steps-
1. New node (Ptr)
Head
INSERT AN ELEMENT AT LAST
prev info NULLprev info nextNULL info next
info data next
srt
Ptr
Steps-
1. New node (Ptr)
2. Ptr → Info := data
Head
INSERT AN ELEMENT AT LAST
prev infoprev info nextNULL info next
data
srt
Ptr
Steps-
1. New node (Ptr)
2. Ptr → Info := data
3. Ptr → prev := NULL
4. Ptr → next := NULL
5. srt → next := ptr
6. Ptr → prev := srt
NULL NULL
NULL
Head
EXAMPLE OF LAST INSERTION:
This is a doubly linked list …. Insert 45 at the last of the list
0x81 8 XX 1 0x81 0x80 5 0x82
After insertion 45 at last….
0x820x810x80
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
X 45 X
0x90
0X820X90
INSERT BEFORE A GIVEN ELEMENT
prev info NULLprev info nextprev info next
Steps-
1. New Node(Ptr)
2. ptr →info = data
NULL info next
data
given element
Ptr
srttemp
Head
INSERT BEFORE A GIVEN ELEMENT
prev info NULLprev info nextinfo next
Steps-
1. New Node(Ptr)
2. ptr →info = data
3. srt →prev := Ptr
4. temp →next := Ptr
5. ptr →prev := temp
6. ptr →next := srt
NULL info
data
given element
Ptr
srttemp
next prev
Head
EXAMPLE INSERT BEFORE A GIVEN ELEMENT:
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
X 36 X
0x90
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
0x90
Inserting 36 before 5…
EXAMPLE INSERT BEFORE A GIVEN ELEMENT:
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
X 36 X
0x90
0x81 8 XX 1 0x80 5 0x82
0x820x810x80
0x90
Inserting 36 before 5…
0x90
EXAMPLE INSERT BEFORE A GIVEN ELEMENT:
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
X 36 X
0x90
0x81 8 XX 1 5 0x82
0x820x810x80
0x90
Inserting 36 before 5…
0x90 0x90
0X80 0X81
SPECIAL CASE: (IF GIVEN ELEMENT IS THE FIRST NODE
OF THE LIST)
prev info NULLprev info nextprev info nextNULL info next
Given element
 When Head →info = given
element
 In this case it is similar to first
insertion ….
Head
INSERT AFTER A GIVEN ELEMENT
prev info NULLprev info nextprev info nextNULL info next
data
given element
srt
Head
Steps-
1. New Node (Ptr)
2. Ptr → info := data
Ptr
INSERT AFTER A GIVEN ELEMENT
prev info NULLinfo nextprev infoNULL info next
data
given element
srt
Head
Steps-
1. New Node (Ptr)
2. Ptr → info := data
3. Ptr → prev := srt
4. Ptr → next := srt → next
5. srt → next →prev := Ptr
6. srt → next := Ptr
Ptr
next prev
EXAMPLE INSERT AFTER A GIVEN ELEMENT:
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
X 10 X
0x90
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
Inserting 10 after 5…
0X820X81
EXAMPLE INSERT AFTER A GIVEN ELEMENT:
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
X 10 X
0x90
0x81 8 XX 1 0x81 0x80 5
0x820x810x80
Inserting 10 after 5…
0X820X81
0x90
EXAMPLE INSERT AFTER A GIVEN ELEMENT:
0x81 8 XX 1 0x81 0x80 5 0x82
0x820x810x80
X 10 X
0x90
8 XX 1 0x81 0x80 5
0x820x810x80
Inserting 10 after 5…
0X820X81
0x90 0x90
SPECIAL CASE: (IF GIVEN ELEMENT IS THE LAST NODE
OF THE LIST)
prev info NULLprev info nextprev info nextNULL info next
Given element
 When srt → next = NULL
 In this case it is similar to last
insertion ….
Head
srt
Now It’s Time for Deletion
DELETE AN ELEMENT AT FIRST
Prev Info NextPrev Info NextNULL Info Next Prev Info NULL
Steps-
1. Set head := head->next
2. Set head->prev := NULL
Head
Head
DELETE AN ELEMENT AT FIRST
Prev Info NextInfo NextNULL Info Next Prev Info NULL
Steps-
1. Set head := head->next
2. Set head->prev := NULL
Head
NULL
EXAMPLE OF FIRST DELETION:
This is a doubly linked list …. Delete the first node..
0x820x810x80
0x81 8 0x83NULL 1 0x81 0x80 5 0x82 0x82 45 NULL
0x83
Head Head
EXAMPLE OF FIRST DELETION:
After deleting first node
0x820x81
0x81 8 0x83NULL 1 0x81 5 0x82 0x82 45 NULL
0x83
Head
NULL
0x80
DELETE AN ELEMENT AT LAST
Prev Info NextPrev Info NextNULL Info Next Prev Info NULL
Steps-
When srt->next := NULL
1. Set temp->next := NULL
Head
Temp Srt
DELETE AN ELEMENT AT LAST
Prev InfoPrev Info NextNULL Info Next Prev Info NULL
Steps-
When srt->next := NULL
1. Set temp->next := NULL
Head
Temp Srt
NULL
EXAMPLE OF LAST DELETION:
This is a doubly linked list …. Delete the last node..
0x820x810x80
0x81 8 0x83NULL 1 0x81 0x80 5 0x82 0x82 45 NULL
0x83
Head Temp Srt
EXAMPLE OF LAST DELETION:
This is a doubly linked list …. Delete the last node..
0x820x810x80
0x81 8NULL 1 0x81 0x80 5 0x82 0x82 45 NULL
0x83
Head Temp Srt
NULL
DELETE A NODE OF GIVEN ELEMENT
Prev Info NextPrev info NextNULL Info Next Prev Info NULL
Head
Steps-
when srt->info := item
srt->next->prev := srt->prev
srt->prev->next := srt->next
Srt
EXAMPLE OF DELETION OF A GIVEN ELEMENT:
This is a doubly linked list …. Delete the particular node.. Suppose the item is 8
0x820x810x80
0x81 5 0x83NULL 1 0x81 0x80 5 0x82 0x82 45 NULL
0x83
8
Head Srt
EXAMPLE OF DELETION OF A GIVEN ELEMENT:
This is a doubly linked list …. Delete the particular node.. Suppose the item is 8
0x820x810x80
0x80 5 0x83NULL 1 0x81 0x80 5 0x82 0x82 45 NULL
0x83
8
Head Srt
EXAMPLE OF DELETION OF A GIVEN ELEMENT:
After deleting the particular node
0x820x810x80
0x80 5 0x83NULL 1 0x80 5 0x82 0x82 45 NULL
0x83
8
Head
0x82
Srt
SPECIAL CASES: FOR DELETION OF GIVEN ELEMENT
I) If the given element is in the first node, it will be as same as first
element delete.
II) If the given element is in the last node, it will be as same as last
element delete.
DELETE A NODE AFTER GIVEN ELEMENT
Prev Info NextPrev Info NextNULL Info Next Prev Info NULL
Steps-
when srt->info := item
Set srt->next->next->prev := srt
srt->next=srt->next->next
Head Srt
EXAMPLE OF DELETION AFTER A GIVEN ELEMENT:
This is a doubly linked list …. Delete the particular node.. Suppose the item is 1
0x820x810x80
0x81 8 0x83NULL 0x81 0x80 5 0x82 0x82 45 NULL
0x83
Head
1
Srt
EXAMPLE OF DELETION AFTER A GIVEN ELEMENT:
This is a doubly linked list …. Delete the particular node.. Suppose the item is 1
0x820x810x80
0x80 8 0x83NULL 0x81 0x80 5 0x82 0x82 45 NULL
0x83
Head
1
Srt
EXAMPLE OF DELETION AFTER A GIVEN ELEMENT:
Deleting the particular node after the given element
0x820x810x80
0x80 8 0x83NULL 0x80 5 0x82 0x82 45 NULL
0x83
Head
0x821
Srt
SPECIAL CASES: FOR DELETION AFTER GIVEN ELEMENT
I) If the given element is before the last node , it will be as same as last element
delete.
II) If the given element is in the last node, it will not be possible. As the
next of last node is NULL , it will not delete anything
DELETE A NODE BEFORE GIVEN ELEMENT
Prev Info NextPrev Info NextNULL Info Next Prev Info NULL
Steps-
when srt->info := item
Set srt->prev->prev->next := srt
srt->prev:= srt-> prev->prev
Head Srt
EXAMPLE OF DELETION BEFORE A GIVEN ELEMENT:
This is a doubly linked list …. Delete the particular node before a given item.. Suppose the item is 9
0x820x810x80
0x81 8 0x83NULL 1 0x81 0x80 5 0x82 0x82 NULL
0x83
Head
9
Srt
EXAMPLE OF DELETION BEFORE A GIVEN ELEMENT:
This is a doubly linked list …. Delete the particular node before a given item.. Suppose the item is 9
0x820x810x80
0x81 8 0x83NULL 1 0x81 0x80 5 0x83 0x82 NULL
0x83
Head
9
Srt
EXAMPLE OF DELETION BEFORE A GIVEN ELEMENT:
After Deleting The Node Before A Given Element
0x820x810x80
0x81 8 0x83NULL 1 0x81 0x80 5 0x83 NULL
0x83
Head
90x81
Srt
SPECIAL CASES: FOR DELETION AFTER GIVEN ELEMENT
I) If the given element is after the first node , it will be as same as first element
delete.
II) If the given element is in the first node, it will not be possible. As the
next of first node is NULL , it will not delete anything
COMPLEXITY – FIRST / LAST INSERTION
prev info NULLprev info nextNULL info next
Head
First insertion
Last insertion
Last
COMPLEXITY – INSERTION
prev info NULLprev info nextNULL info next
Head
insertion
Last
COMPLEXITY – FIRST / LAST DELETION
prev info NULLprev info nextNULL info next
Head
First deletion
Last deletion
Last
COMPLEXITY – DELETION
prev info NULLprev info nextNULL info next
Head
deletion
Last
COMPLEXITY OF DOUBLY LINKED LIST
• Inserting based on the value ( e.g. inserting in a sorted list )- will be O(n). If it
is being inserted after or before an existing known node is O(1).
• Deleting an arbitrary value (rather than a node) will indeed be O(n) as it will
need to find the value. Deleting a node (when the node is known ) is O(1).
• Inserting to the first or last of the list will always be O(1) - because those are
just special cases of the above.
ADVANTAGES OF DOUBLY LINKED LIST
 Dynamic size
 Ease of insertion/deletion
 A DLL can be traversed in both forward and backward direction.
 The delete operation in DLL is more efficient if pointer to the node to be
deleted is given.
In singly linked list, to delete a node, pointer to the previous node is needed.
To get this previous node, sometimes the list is traversed. In DLL, we can get
the previous node using previous pointer.
DISADVANTAGES OF DOUBLY LINKED LIST
 Every node of DLL Require extra space for an previous pointer.
 All operations require an extra pointer previous to be maintained. For
example, in insertion, we need to modify previous pointers together with next
pointers.
REFERENCES:
• Class lecture
• Stackoverflow.com
• Geekforgeeks.org
Thank you

11 15 (doubly linked list)

  • 1.
    DOUBLY LINKED LIST CREATEDBY: 1507011- TAHMINA RAHMAN 1507012- SAMIUL AZIM 1507013- FABLIHA HAQUE 1507014- SALIM SHADMAN ANKUR 1507015- SAIFUR RAHMAN
  • 2.
    TOPICS: • linked lists •doubly linked list • Creation and traversing of doubly linked list • Insertion and deletion on doubly linked list • complexity of insertion and deletion • Advantages and disadvantages of doubly linked list
  • 3.
    LINKED LIST  Likearrays, Linked List is a linear data structure  Unlike arrays, linked list elements are not stored at contiguous location  the elements are linked using pointers  does not have any index
  • 4.
    ADDRESSES FOR LINKEDLIST AND ARRAY Linked List Arrray
  • 5.
    WHY WE USELINKED LIST Arrays can be used to store linear data of similar types, but arrays have following limitations.. 1) The size of the arrays is fixed . 2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.
  • 6.
    DOUBLY LINKED LIST •A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list. • Every nodes in the doubly linked list has three fields: 1. Left Pointer (prev) 2. Right Pointer (next) 3. Data (info)
  • 7.
    DOUBLY LINKED LIST •Prev - address of the previous node • Next - address of the next node • Info - information of the node • head - address of the first node Prev Info Next node
  • 8.
    CREATION OF DOUBLYLINKED LIST Steps- 1: Head := NULL (as global) 2: New Node (Ptr) // set data and prev , next to NULL 3: If Head = NULL  Head := ptr  temp := ptr 4. If not  temp → next := ptr  Ptr → prev := temp  temp := ptr
  • 9.
    CREATION OF DOUBLYLINKED LIST NULL data1 NULL 0x80 0x80 data2 NULLNULL data1 0x81 0x810x80 0x80 data2 0x81NULL data1 0x81 0x81 data3 NULL 0x80 0x81 0x82 No element inserted Third element inserted: Second element inserted: First element inserted: Empty list Head temp Head temp Head temp
  • 10.
    TRAVERSING 0x80 9 0x82NULL8 0x81 0x81 10 NULL Head Srt Steps: Set srt := head while ( srt->next != NULL) print srt->info // 8 srt := srt->next if (srt->next == NULL) print srt->info end of traversing 0x80 0x81 0x82 False Not Executed Output: 8 Srt 0x80 0x81 Srt
  • 11.
    TRAVERSING 0x80 9 0x82NULL8 0x81 0x81 10 NULL Head Srt Steps: Set srt := head while ( srt->next != NULL) print srt->info // 9 srt := srt->next if (srt->next = NULL) print srt->info end of traversing 0x80 0x81 0x82 True Output: 8 9 10 Srt 0x80 0x81 0x82 Srt
  • 12.
    OPERATIONS ON DOUBLYLINKED LIST Insertion • Insert an element at first • Insert an element at last • Insert before a given element • Insert after a given element Deletion • Delete first element • Delete last element • Delete a given element • Delete before a given element • Delete after a given element
  • 13.
    INSERT AN ELEMENTAT FIRST prev info NULLprev info nextNULL info next Head Steps- 1. New node (Ptr) prev info next Ptr
  • 14.
    INSERT AN ELEMENTAT FIRST prev info NULLprev info nextinfo next Head data Steps- 1. New node (Ptr) 2. Ptr → info := data 3. Ptr → prev := NULL 4. Ptr → next := NULL Ptr NULL NULL NULL
  • 15.
    INSERT AN ELEMENTAT FIRST prev info NULLprev info nextinfo next Head NULL data Steps- 1. New node (Ptr) 2. Ptr → Info := data 3. Ptr → prev := NULL 4. Ptr → next := NULL 5. Head → prev := ptr 6. Ptr → next := head 7. Head := Ptr Ptr NULL NULL Head
  • 16.
    EXAMPLE OF FIRSTINSERTION: This is a doubly linked list …. 0x81 8 XX 1 0x81 0x80 5 0x82 Inserting 45 at the first of the list 0x820x810x80 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 X 45 X 0x90 0X900X80
  • 17.
    INSERT AN ELEMENTAT LAST prev info NULLprev info nextNULL info next prev info next srt Ptr Steps- 1. New node (Ptr) Head
  • 18.
    INSERT AN ELEMENTAT LAST prev info NULLprev info nextNULL info next info data next srt Ptr Steps- 1. New node (Ptr) 2. Ptr → Info := data Head
  • 19.
    INSERT AN ELEMENTAT LAST prev infoprev info nextNULL info next data srt Ptr Steps- 1. New node (Ptr) 2. Ptr → Info := data 3. Ptr → prev := NULL 4. Ptr → next := NULL 5. srt → next := ptr 6. Ptr → prev := srt NULL NULL NULL Head
  • 20.
    EXAMPLE OF LASTINSERTION: This is a doubly linked list …. Insert 45 at the last of the list 0x81 8 XX 1 0x81 0x80 5 0x82 After insertion 45 at last…. 0x820x810x80 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 X 45 X 0x90 0X820X90
  • 21.
    INSERT BEFORE AGIVEN ELEMENT prev info NULLprev info nextprev info next Steps- 1. New Node(Ptr) 2. ptr →info = data NULL info next data given element Ptr srttemp Head
  • 22.
    INSERT BEFORE AGIVEN ELEMENT prev info NULLprev info nextinfo next Steps- 1. New Node(Ptr) 2. ptr →info = data 3. srt →prev := Ptr 4. temp →next := Ptr 5. ptr →prev := temp 6. ptr →next := srt NULL info data given element Ptr srttemp next prev Head
  • 23.
    EXAMPLE INSERT BEFOREA GIVEN ELEMENT: 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 X 36 X 0x90 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 0x90 Inserting 36 before 5…
  • 24.
    EXAMPLE INSERT BEFOREA GIVEN ELEMENT: 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 X 36 X 0x90 0x81 8 XX 1 0x80 5 0x82 0x820x810x80 0x90 Inserting 36 before 5… 0x90
  • 25.
    EXAMPLE INSERT BEFOREA GIVEN ELEMENT: 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 X 36 X 0x90 0x81 8 XX 1 5 0x82 0x820x810x80 0x90 Inserting 36 before 5… 0x90 0x90 0X80 0X81
  • 26.
    SPECIAL CASE: (IFGIVEN ELEMENT IS THE FIRST NODE OF THE LIST) prev info NULLprev info nextprev info nextNULL info next Given element  When Head →info = given element  In this case it is similar to first insertion …. Head
  • 27.
    INSERT AFTER AGIVEN ELEMENT prev info NULLprev info nextprev info nextNULL info next data given element srt Head Steps- 1. New Node (Ptr) 2. Ptr → info := data Ptr
  • 28.
    INSERT AFTER AGIVEN ELEMENT prev info NULLinfo nextprev infoNULL info next data given element srt Head Steps- 1. New Node (Ptr) 2. Ptr → info := data 3. Ptr → prev := srt 4. Ptr → next := srt → next 5. srt → next →prev := Ptr 6. srt → next := Ptr Ptr next prev
  • 29.
    EXAMPLE INSERT AFTERA GIVEN ELEMENT: 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 X 10 X 0x90 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 Inserting 10 after 5… 0X820X81
  • 30.
    EXAMPLE INSERT AFTERA GIVEN ELEMENT: 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 X 10 X 0x90 0x81 8 XX 1 0x81 0x80 5 0x820x810x80 Inserting 10 after 5… 0X820X81 0x90
  • 31.
    EXAMPLE INSERT AFTERA GIVEN ELEMENT: 0x81 8 XX 1 0x81 0x80 5 0x82 0x820x810x80 X 10 X 0x90 8 XX 1 0x81 0x80 5 0x820x810x80 Inserting 10 after 5… 0X820X81 0x90 0x90
  • 32.
    SPECIAL CASE: (IFGIVEN ELEMENT IS THE LAST NODE OF THE LIST) prev info NULLprev info nextprev info nextNULL info next Given element  When srt → next = NULL  In this case it is similar to last insertion …. Head srt
  • 33.
    Now It’s Timefor Deletion
  • 34.
    DELETE AN ELEMENTAT FIRST Prev Info NextPrev Info NextNULL Info Next Prev Info NULL Steps- 1. Set head := head->next 2. Set head->prev := NULL Head Head
  • 35.
    DELETE AN ELEMENTAT FIRST Prev Info NextInfo NextNULL Info Next Prev Info NULL Steps- 1. Set head := head->next 2. Set head->prev := NULL Head NULL
  • 36.
    EXAMPLE OF FIRSTDELETION: This is a doubly linked list …. Delete the first node.. 0x820x810x80 0x81 8 0x83NULL 1 0x81 0x80 5 0x82 0x82 45 NULL 0x83 Head Head
  • 37.
    EXAMPLE OF FIRSTDELETION: After deleting first node 0x820x81 0x81 8 0x83NULL 1 0x81 5 0x82 0x82 45 NULL 0x83 Head NULL 0x80
  • 38.
    DELETE AN ELEMENTAT LAST Prev Info NextPrev Info NextNULL Info Next Prev Info NULL Steps- When srt->next := NULL 1. Set temp->next := NULL Head Temp Srt
  • 39.
    DELETE AN ELEMENTAT LAST Prev InfoPrev Info NextNULL Info Next Prev Info NULL Steps- When srt->next := NULL 1. Set temp->next := NULL Head Temp Srt NULL
  • 40.
    EXAMPLE OF LASTDELETION: This is a doubly linked list …. Delete the last node.. 0x820x810x80 0x81 8 0x83NULL 1 0x81 0x80 5 0x82 0x82 45 NULL 0x83 Head Temp Srt
  • 41.
    EXAMPLE OF LASTDELETION: This is a doubly linked list …. Delete the last node.. 0x820x810x80 0x81 8NULL 1 0x81 0x80 5 0x82 0x82 45 NULL 0x83 Head Temp Srt NULL
  • 42.
    DELETE A NODEOF GIVEN ELEMENT Prev Info NextPrev info NextNULL Info Next Prev Info NULL Head Steps- when srt->info := item srt->next->prev := srt->prev srt->prev->next := srt->next Srt
  • 43.
    EXAMPLE OF DELETIONOF A GIVEN ELEMENT: This is a doubly linked list …. Delete the particular node.. Suppose the item is 8 0x820x810x80 0x81 5 0x83NULL 1 0x81 0x80 5 0x82 0x82 45 NULL 0x83 8 Head Srt
  • 44.
    EXAMPLE OF DELETIONOF A GIVEN ELEMENT: This is a doubly linked list …. Delete the particular node.. Suppose the item is 8 0x820x810x80 0x80 5 0x83NULL 1 0x81 0x80 5 0x82 0x82 45 NULL 0x83 8 Head Srt
  • 45.
    EXAMPLE OF DELETIONOF A GIVEN ELEMENT: After deleting the particular node 0x820x810x80 0x80 5 0x83NULL 1 0x80 5 0x82 0x82 45 NULL 0x83 8 Head 0x82 Srt
  • 46.
    SPECIAL CASES: FORDELETION OF GIVEN ELEMENT I) If the given element is in the first node, it will be as same as first element delete. II) If the given element is in the last node, it will be as same as last element delete.
  • 47.
    DELETE A NODEAFTER GIVEN ELEMENT Prev Info NextPrev Info NextNULL Info Next Prev Info NULL Steps- when srt->info := item Set srt->next->next->prev := srt srt->next=srt->next->next Head Srt
  • 48.
    EXAMPLE OF DELETIONAFTER A GIVEN ELEMENT: This is a doubly linked list …. Delete the particular node.. Suppose the item is 1 0x820x810x80 0x81 8 0x83NULL 0x81 0x80 5 0x82 0x82 45 NULL 0x83 Head 1 Srt
  • 49.
    EXAMPLE OF DELETIONAFTER A GIVEN ELEMENT: This is a doubly linked list …. Delete the particular node.. Suppose the item is 1 0x820x810x80 0x80 8 0x83NULL 0x81 0x80 5 0x82 0x82 45 NULL 0x83 Head 1 Srt
  • 50.
    EXAMPLE OF DELETIONAFTER A GIVEN ELEMENT: Deleting the particular node after the given element 0x820x810x80 0x80 8 0x83NULL 0x80 5 0x82 0x82 45 NULL 0x83 Head 0x821 Srt
  • 51.
    SPECIAL CASES: FORDELETION AFTER GIVEN ELEMENT I) If the given element is before the last node , it will be as same as last element delete. II) If the given element is in the last node, it will not be possible. As the next of last node is NULL , it will not delete anything
  • 52.
    DELETE A NODEBEFORE GIVEN ELEMENT Prev Info NextPrev Info NextNULL Info Next Prev Info NULL Steps- when srt->info := item Set srt->prev->prev->next := srt srt->prev:= srt-> prev->prev Head Srt
  • 53.
    EXAMPLE OF DELETIONBEFORE A GIVEN ELEMENT: This is a doubly linked list …. Delete the particular node before a given item.. Suppose the item is 9 0x820x810x80 0x81 8 0x83NULL 1 0x81 0x80 5 0x82 0x82 NULL 0x83 Head 9 Srt
  • 54.
    EXAMPLE OF DELETIONBEFORE A GIVEN ELEMENT: This is a doubly linked list …. Delete the particular node before a given item.. Suppose the item is 9 0x820x810x80 0x81 8 0x83NULL 1 0x81 0x80 5 0x83 0x82 NULL 0x83 Head 9 Srt
  • 55.
    EXAMPLE OF DELETIONBEFORE A GIVEN ELEMENT: After Deleting The Node Before A Given Element 0x820x810x80 0x81 8 0x83NULL 1 0x81 0x80 5 0x83 NULL 0x83 Head 90x81 Srt
  • 56.
    SPECIAL CASES: FORDELETION AFTER GIVEN ELEMENT I) If the given element is after the first node , it will be as same as first element delete. II) If the given element is in the first node, it will not be possible. As the next of first node is NULL , it will not delete anything
  • 57.
    COMPLEXITY – FIRST/ LAST INSERTION prev info NULLprev info nextNULL info next Head First insertion Last insertion Last
  • 58.
    COMPLEXITY – INSERTION previnfo NULLprev info nextNULL info next Head insertion Last
  • 59.
    COMPLEXITY – FIRST/ LAST DELETION prev info NULLprev info nextNULL info next Head First deletion Last deletion Last
  • 60.
    COMPLEXITY – DELETION previnfo NULLprev info nextNULL info next Head deletion Last
  • 61.
    COMPLEXITY OF DOUBLYLINKED LIST • Inserting based on the value ( e.g. inserting in a sorted list )- will be O(n). If it is being inserted after or before an existing known node is O(1). • Deleting an arbitrary value (rather than a node) will indeed be O(n) as it will need to find the value. Deleting a node (when the node is known ) is O(1). • Inserting to the first or last of the list will always be O(1) - because those are just special cases of the above.
  • 62.
    ADVANTAGES OF DOUBLYLINKED LIST  Dynamic size  Ease of insertion/deletion  A DLL can be traversed in both forward and backward direction.  The delete operation in DLL is more efficient if pointer to the node to be deleted is given. In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.
  • 63.
    DISADVANTAGES OF DOUBLYLINKED LIST  Every node of DLL Require extra space for an previous pointer.  All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers.
  • 64.
    REFERENCES: • Class lecture •Stackoverflow.com • Geekforgeeks.org
  • 65.

Editor's Notes

  • #3 Hello good afternoon….. I am ..... I am here to make a presentation on doubly linked list These are some parts of this presentation
  • #4 at first I am going to talk about linked list… What is a linked list?? Why we need to use linked list?? I will tell you something behind the figure….
  • #5 Variables or pointers addresses of a node are contiguous Bt every node’s starting address is random….not sequential. Now… look at the memory allocation of array…. The addresses are contiguous. So that’s the difference
  • #6 Why linked list?? Well, we can increase the size of the list Bt we can not increase the size of an initialized sized array …. If it is attempted , then index wont be found and cause overflow
  • #7 A double way or two way linked list a linked list that every element is linked with its previous and next element through pointers
  • #9 To create a doubly linked list.. Firstly a pointer of node type is declared globally …. Say it is “head”. And initialized to null, as it points to nothing. Now each time a new element is inserted, a new node is created (here denoted by ptr). Data is stored in the info section of ptr And prev and next pointer initialized to null Now for the first node…. Head needs to point the first node…. Also for the next insertion the previous node address needs to be stored… That’s why temp is used to store the address. Then for all other insertion … We set the next pointer of temp to ptr; And prev pointer of ptr to temp; Update temp to ptr to keep track of the previous node.
  • #11 For traversing we start from the head and continue till the next pointer of a node equals to null... And