lecture 07 doubly linked list - learn with mubashir · doubly linked list doubly linked list is a...
TRANSCRIPT
-
Doubly Linked List
Mr. Mubashir AliLecturer (Dept. of Software Engineering)
[email protected] Garrison University, Lahore
1
Lecture 07
mailto:[email protected]
-
Previous Lecture
DeleteFromHead(x) DeleteFromTail(x) Delete(x) Implementation
Delete a Particular Node Update Node Value Search Element Display Linked List Sort Link List Reverse Linked List
Application of Linked List Lab-3
Mubashir Ali - Lecturer (Department of Software Engineering)
2
-
Outline
1. Doubly Linked List
2. Advantages over Singly Linked List
3. Disadvantages over Singly Linked List
4. Operations on Doubly Linked List
5. Add to Head
6. Add to Tail
7. Add to Specific Position
Mubashir Ali - Lecturer (Department of Software Engineering)
3
-
Doubly Linked List
Doubly linked list is a complex type of linked list inwhich a node contains a pointer to the previous as wellas the next node in the sequence. Therefore, in adoubly linked list, a node consists of three parts: nodedata, pointer to the next node in sequence (nextpointer) , pointer to the previous node (previouspointer).
Mubashir Ali - Lecturer (Department of Software Engineering)
4
-
Doubly Linked List
Mubashir Ali - Lecturer (Department of Software Engineering)
5
-
Node in Doubly Linked List
Mubashir Ali - Lecturer (Department of Software Engineering)
6
-
Advantages over Singly Linked List
• A DLL can be traversed in both forward andbackward direction.
• The delete operation in DLL is more efficient ifpointer to the node to be deleted is given.
• We can quickly insert a new node before a givennode.
In singly linked list, to delete a node, pointer to theprevious node is needed. To get this previous node,sometimes the list is traversed. In DLL, we can get theprevious node using previous pointer.
Mubashir Ali - Lecturer (Department of Software Engineering)
7
-
Disadvantages over Singly Linked List
• Every node of DLL Require extra space for anprevious pointer. It is possible to implement DLLwith single pointer?
• All operations require an extra pointer previous tobe maintained. For example, in insertion, we needto modify previous pointers together with nextpointers. For example in following functions forinsertions at different positions, we need 1 or 2extra steps to set previous pointer.
Mubashir Ali - Lecturer (Department of Software Engineering)
8
-
Operations on Doubly Linked List
Operation Description
Insertion at
beginning
Adding the node into the linked list at
beginning.
Insertion at end Adding the node into the linked list to the
end.
Insertion after
specified node
Adding the node into the linked list after the
specified node.
Deletion at
beginning
Removing the node from beginning of the list
Deletion at the
end
Removing the node from end of the list.
Mubashir Ali - Lecturer (Department of Software Engineering)
9
-
Operations on Doubly Linked List
Operation Description
Deletion of the
node having given
data
Removing the node which is present just after
the node containing the given data.
Searching Comparing each node data with the item to
be searched and return the location of the
item in the list if the item found else return
null.
Traversing Visiting each node of the list at least once in
order to perform some specific operation like
searching, sorting, display, etc. or as many
operations as you want.Mubashir Ali - Lecturer (Department of
Software Engineering)10
-
Add to Head
There are two scenarios of inserting any element intodoubly linked list.
• Either the list is empty
• It contains at least one element
Perform the following steps to insert a node in doublylinked list at beginning.
• Allocate the space for the new node in the memory.
• Check whether the list is empty or not. The list is empty if thecondition head == NULL holds. In that case, the node will beinserted as the only node of the list and therefore the prevand the next pointer of the node will point to NULL and thehead pointer will point to this node.
Mubashir Ali - Lecturer (Department of Software Engineering)
11
-
Add to Head
• In the second scenario, the condition head ==NULL become false and the node will be inserted inbeginning. The next pointer of the node will point tothe existing head pointer of the node. The prevpointer of the existing head will point to the newnode being inserted.
• Since, the node being inserted is the first node of thelist and therefore it must contain NULL in its prevpointer. Hence assign null to its previous part andmake the head point to this node.
Mubashir Ali - Lecturer (Department of Software Engineering)
12
-
Add to Head
Mubashir Ali - Lecturer (Department of Software Engineering)
13
-
Add to Head – Algorithm
• Step 1: IF ptr = NULL
Write OVERFLOW - Go to Step 9 - [END OF IF]
• Step 2: SET NEW_NODE = ptr
• Step 3: SET ptr = ptr -> 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 head -> PREV = NEW_NODE
• Step 8: SET head = NEW_NODE
• Step 9: EXIT
Mubashir Ali - Lecturer (Department of Software Engineering)
14
-
Add to Head – Implementation in C
Mubashir Ali - Lecturer (Department of Software Engineering)
15
-
Add to Tail
• Allocate the memory for the new node.
• Check whether the list is empty or not. The list is empty ifthe condition head == NULL holds. In that case, the nodewill be inserted as the only node of the list and thereforethe prev and the next pointer of the node will point toNULL and the head pointer will point to this node.
• In the second scenario, the condition head == NULLbecome false. The new node will be inserted as the lastnode of the list. For this purpose, we have to traverse thewhole list in order to reach the last node of the list.Initialize the pointer temp to head and traverse the listby using this pointer.
Mubashir Ali - Lecturer (Department of Software Engineering)
16
-
Add to Tail
• The pointer temp point to the last node at the end ofthis while loop. Now, we just need to make a fewpointer adjustments to insert the new node ptr tothe list. First, make the next pointer of temp point tothe new node being inserted i.e. ptr.
• Make the previous pointer of the node ptr point to the existing last node of the list i.e. temp.
• Make the next pointer of the node ptr point to the null as it will be the new last node of the list.
Mubashir Ali - Lecturer (Department of Software Engineering)
17
-
Add to Tail
Mubashir Ali - Lecturer (Department of Software Engineering)
18
-
Add to Tail – Algorithm
• Step 1: IF PTR = NULL
– Write OVERFLOW - Go to Step 11 - [END OF IF]
• Step 2: SET NEW_NODE = PTR
• Step 3: SET PTR = PTR -> NEXT
• Step 4: SET NEW_NODE -> DATA = VAL
• Step 5: SET NEW_NODE -> NEXT = NULL
• Step 6: SET TEMP = START
• Step 7: Repeat Step 8 while TEMP -> NEXT != NULL
• Step 8: SET TEMP = TEMP -> NEXT
– [END OF LOOP]
• Step 9: SET TEMP -> NEXT = NEW_NODE
• Step 10C: SET NEW_NODE -> PREV = TEMP
• Step 11: EXIT
Mubashir Ali - Lecturer (Department of Software Engineering)
19
-
Add to Tail – Implementation in C
Mubashir Ali - Lecturer (Department of Software Engineering)
20
-
Add to Specific Position
• In order to insert a node after the specified node inthe list, we need to skip the required number ofnodes in order to reach the mentioned node andthen make the pointer adjustments as required.
• Allocate the memory for the new node.
• Traverse the list by using the pointer temp to skip therequired number of nodes in order to reach thespecified node.
Mubashir Ali - Lecturer (Department of Software Engineering)
21
-
Add to Specific Position
• The temp would point to the specified node at theend of the for loop. The new node needs to beinserted after this node therefore we need to make afer pointer adjustments here. Make the next pointerof ptr point to the next node of temp.
• Make the prev of the new node ptr point to temp.
• Make the next pointer of temp point to the newnode ptr.
• Make the previous pointer of the next node of temppoint to the new node.
Mubashir Ali - Lecturer (Department of Software Engineering)
22
-
Add to Specific Position
Mubashir Ali - Lecturer (Department of Software Engineering)
23
-
Add to Specific Position – Algorithm
• Step 1: IF PTR = NULL
Write OVERFLOW - Go to Step 15 - [END OF IF]
• Step 2: SET NEW_NODE = PTR
• Step 3: SET PTR = PTR -> NEXT
• Step 4: SET NEW_NODE -> DATA = VAL
• Step 5: SET TEMP = START
• Step 6: SET I = 0
• Step 7: REPEAT 8 to 10 until I
• Step 8: SET TEMP = TEMP -> NEXT
Mubashir Ali - Lecturer (Department of Software Engineering)
24
-
Add to Specific Position – Algorithm
• STEP 9: IF TEMP = NULL
• STEP 10: WRITE "LESS THAN DESIRED NO. OF ELEMENTS“
– GOTO STEP 15 - [END OF IF] - [END OF LOOP]
• Step 11: SET NEW_NODE -> NEXT = TEMP -> NEXT
• Step 12: SET NEW_NODE -> PREV = TEMP
• Step 13 : SET TEMP -> NEXT = NEW_NODE
• Step 14: SET TEMP -> NEXT -> PREV = NEW_NODE
• Step 15: EXIT
Mubashir Ali - Lecturer (Department of Software Engineering)
25
-
Add to Specific Position –Implementation in C
Mubashir Ali - Lecturer (Department of Software Engineering)
26
-
Lab-4
Write C++ program to perform dynamicimplementation of Doubly Linked List. Writefunction for add data to head, tail andspecific position.
Mubashir Ali - Lecturer (Department of Software Engineering)
27
-
Summary
Doubly Linked List
Advantages over Singly Linked List
Disadvantages over Singly Linked List
Operations on Doubly Linked List
Add to Head
Add to Tail
Add to Specific Position
Mubashir Ali - Lecturer (Department of Software Engineering)
28
-
References
you will be able to find course resources at
http://www.mubashirali.com/data-structures-algorithms/
Topic 3.2 (Data Structures and Algorithms in C++ by AdamDrozdek)
Mubashir Ali - Lecturer (Department of Software Engineering)
29
http://www.mubashirali.com/data-structures-algorithms/