{"id":52,"date":"2018-07-18T08:56:43","date_gmt":"2018-07-18T08:56:43","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=52"},"modified":"2018-12-12T07:08:27","modified_gmt":"2018-12-12T07:08:27","slug":"linked-list-implementation-of-list-adt","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/linked-list-implementation-of-list-adt\/","title":{"rendered":"Linked List Implementation of List ADT"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/hsp3Nz80jjk\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures.This is the fourth module of the course and we will be talking about the linked list implementation of List ADT.<\/p>\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe learning objectives of this module are as follows:\r\n\r\n&nbsp;\r\n\r\n\u2022 To understand Linked List Implementation of List ADT\r\n<p style=\"text-align: justify\">\u2022 To explore the singly linked list, the circular list and the doubly linked list implementations of List ADT<\/p>\r\n\u2022 To know about the representation of a polynomial as a linked list\r\n\r\n&nbsp;\r\n\r\n<strong>4.1 Linked List Implementation (Pointer)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Sometimes it is not possible or realistic to ensure that the list is stored contiguously as it may not be possible to determine the size of the list required beforehand. We may end up wasting space because we have overestimated the requirements. In such cases we use a linked list. In this case we have a series of structures or locations that are not necessarily adjacent in memory. Compared to the array implementation, the pointer implementation uses only as much space as is needed for the elements currently on the listbut however requires additional space for the pointers in each cell of the linked list.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Linked lists require some additional steps of code during implementation, but however have some specific advantages as the linked list is dynamic as they can easily grow and shrink in size.We don\u2019t need to know how many nodes will be in the list. The locations necessary for the linked list are obtained as when needed.<\/p>\r\n&nbsp;\r\n\r\n<strong>4.2 Easy and Fast Insertions and Deletions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To insert or delete an element in an array what is needed is to copy to temporary variables to make room for new elements and close the gap caused by deleted elements.With a linked list, there is no need to move other nodes. We need to only reset some pointers. In other words for a linked list implementation there is no need to estimate the maximum size of the list and consequently there is no wasted space. The time taken for some of the common operations such as printList, find and findKth is of the order of O(n) while insert and delete is of the order of O(1). Insert at position 0 (making a new element) or inserting does not require moving the other elements.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Similarly delete at position 0 or deleting requires no shifting of elements. Insertion and deletion becomes easier, but finding the Kth element for the place where insertion or deletion needs to be carried out requires time of the order of O(n).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: left\"><strong>4.3 Linked Lists<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A linked listis essentially a series of nodes connected by links where each node contains some data (any type) and apointer to the next node in the list. In some cases there may be a <strong><em>Head<\/em><\/strong>whichis a pointer to the first node. The Head node has null data value with its pointer pointing to the first node in the list. For convenience we do not show the contents of the head node except the pointer to the first node in the list.The last node of a linked list normally points to NULL(Figure 4.1). Note, that the nodes in a linked list can be <strong>spread out <\/strong>over the memory and need not occupy contiguous memory locations. In all the discussions regarding insertion and deletion operations on lists we assume that the list is not empty and hence we do not discuss boundary conditions.<\/p>\r\n<p style=\"text-align: center\"><img class=\"alignnone size-full wp-image-55 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-8.png\" alt=\"\" width=\"699\" height=\"257\" \/><\/p>\r\n<p style=\"text-align: center\"><strong>4.3 Insertion- The Scenario<\/strong><\/p>\r\n<p style=\"text-align: justify\">Let us study the insertion scenario in the case of linked lists. You have a linked list which may be empty or not, may beordered, or not. You want to add an element into the given linked list (Figure 4.2).<\/p>\r\n<img class=\"alignnone size-full wp-image-56 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-9.png\" alt=\"\" width=\"639\" height=\"134\" \/>\r\n<p style=\"text-align: justify\">When inserting a new node into a linked list, there are four possible cases as described below:<\/p>\r\n&nbsp;\r\n\r\n1. Insert into an empty list\r\n\r\n2.Insert in front\r\n\r\n3. Insert at back\r\n\r\n4. Insert in middle\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">However in fact, we need to handle only two cases. The insert as the first node can handle Case 1 and Case 2. Insert in the middle or at the end of the list Case 3 and Case\u00a0<span style=\"font-size: 1em;text-align: initial\">4can be handled in a similar manner.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0<\/strong>\r\n\r\n<strong> 4.3.1 The Actual Insertion<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe actual insertion into a linked list involves the following steps:\r\n<ul>\r\n \t<li style=\"text-align: justify\">Creation of a new node \u2013We use a function Freenode that gives a node in the structure required to be used by the linked list .<\/li>\r\n \t<li style=\"text-align: justify\">Adding the data \u2013 We add the data to the data component of the node provided by Freenode.<\/li>\r\n \t<li style=\"text-align: justify\">Updating the next pointer of the new node \u2013 We update the Next pointer (pointer pointing to next node in the linked list) of the new node just obtained to point to the appropriate node.<\/li>\r\n \t<li style=\"text-align: justify\">Update current to point to new node \u2013 Now we update the current node\u2019s (obtained through appropriate search procedure) next pointer to point to the new node.<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 \u00a0 \u00a0 4.3.2 Inserting to the Empty Or to the Front of a Linked List<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-57 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-10.png\" alt=\"\" width=\"597\" height=\"211\" \/>\r\n\r\n<span style=\"text-align: initial;text-indent: 1em;font-size: 1em\">Let us assume that we call inserting into the front of the list as follows:<\/span>\r\n\r\n&nbsp;\r\n\r\n<strong>Insertfront (93, List)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this case there is no work to find the correct location. Empty or not, head will point to the right location. For the list shown in Figure 4.3, the following are the steps (Figure 4.4)<\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-737 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-35.png\" alt=\"\" width=\"795\" height=\"248\" \/><\/p>\r\n\r\n<div><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a04.3.3 Inserting to the End of a List<\/strong><\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 Let us assume that we call inserting to the end of the list as follows:\r\n\r\n&nbsp;\r\n\r\n<strong>Insertend (83, List)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this case there is a need to find the correct location for the insertion to take place. We need to know the address of the current last node in order to connect to the new last node. We needto perform iteration (Figure 4.5) in order to find the address of current last node for which we need a search pointer (P).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-738 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-36.png\" alt=\"\" width=\"775\" height=\"225\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we need to insert a node after the Node P which we have found through the iterative procedure. The steps are shown in Figure 4.6.<\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: center\"><img class=\" wp-image-739 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-37.png\" alt=\"\" width=\"553\" height=\"206\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a04.3.4 Inserting into the Middle of Unsorted List &amp; Sorted List<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this type of insertion also we need to traverse the list recursively until we find the correct place to insert either after a particular given data value or insert at the correct place in a sorted list.<\/p>\r\n&nbsp;\r\n\r\n<strong>4.3.4.1Inserting in the Middle<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The case we will first consider is inserting in the middle of an unsorted list iswhere we insert in the middle of the list because we have found the node containing a data value (17) before which we want to insert the given data (88).<\/p>\r\n&nbsp;\r\n\r\n<strong>Insertbefore (88,17,List)<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">As discussed previously, the operation insert takes us to the iterative procedure for finding the position after which we want to insert or the data value before which we want to insert. Note here that we need the address of the node before the node containing 17 in order to carry out the insertion. This finding of appropriate position is a common need (Figure 4.7). After finding the position P, the node after which we want to insert, we carry out the insertion (Figure 4.8and Figure 4.9)<\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-740 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-38.png\" alt=\"\" width=\"806\" height=\"245\" \/>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 4.7 Steps for Iteratively Finding a Data Item<\/strong><\/p>\r\n<p style=\"text-align: center\"><img class=\" wp-image-741 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-39.png\" alt=\"\" width=\"628\" height=\"228\" \/><\/p>\r\n<img class=\"alignnone size-full wp-image-58 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-11.png\" alt=\"\" width=\"651\" height=\"369\" \/>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-59 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-12.png\" alt=\"\" width=\"615\" height=\"184\" \/>\r\n<p style=\"text-align: justify\">The next case of inserting in the middle of a list is insert a data item into a given sorted list (Figure 4.10) by using the following:<\/p>\r\n&nbsp;\r\n\r\n<strong>Insertsorted(50,List)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For this we need to find the node after which we want to insert the new data so that the sorted order is maintained. Again a iterative procedure where we need to start from Head using a search variable P until we find the address of the node after which we need to insert \u2013 for this we need to ensure that the node after the one we find has value greater than 50 (Figure 4.11). Here we assume the list is in ascending order.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-742 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-40.png\" alt=\"\" width=\"808\" height=\"312\" \/><\/p>\r\n<p style=\"text-align: justify\">Once we have found the location P after which to insert by using the steps indicated in Figure 4.11then the procedure for insertion is similar to insertion in the middle of an unsorted list (Figure 4.9)<\/p>\r\n&nbsp;\r\n\r\n<strong>4.4 Deletion-The Scenario<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For deletion, we begin with an existing linked list that could be empty or not and could be ordered or not. In the case of deletion there are three possible situations:<\/p>\r\n&nbsp;\r\n\r\n\u2022 Delete the first element\r\n\r\n\u2022 Delete the first occurrence of an element\r\n\r\n\u2022 Delete all occurrences of a particular element\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 4.5 The Actual Deletion<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nDeletion involves the following basic steps:\r\n\r\n\u2022 Getting to the correct position \u2013 iteratively finding the correct node to delete\r\n<p style=\"text-align: justify\">\u2022 Moving a pointer so nothing points to the element to be deleted (Figure 4.12 and Figure 4.13)<\/p>\r\n&nbsp;\r\n\r\nWe want to <strong>Delete(40, Head)<\/strong>\r\n\r\n<img class=\"alignnone size-full wp-image-60 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-13.png\" alt=\"\" width=\"628\" height=\"306\" \/>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-743 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-41.png\" alt=\"\" width=\"789\" height=\"255\" \/><\/p>\r\n<strong>\u00a0 \u00a0 4.6 Linked List Traversal &amp; Printing<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In general, traversal means \u201cvisiting\u201d or examining each node. In the case of a singly linked list we generally start at the beginning of the list and go one node at a time until the end. This is a recursive procedure (function) that given a head pointer, looks at just one node at a time.<\/p>\r\n&nbsp;\r\n\r\n<strong>4.7 Circular Linked Lists<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Another representation of the list is a variation of the singly linked list called the circular linked list (Figure 4.14) where the last node points to the first node of the list. One question associated with circular linked lists is how do we know when we have finished traversing the list? In order to solve this problem we need to check if the pointer of the current node is equal to the head. We may need to cycle through a list repeatedlye.g.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\nwhen we use the list to implement a round robin system for a shared resource. In this case having the last node point to the first node is a solution.\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-61 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-14.png\" alt=\"\" width=\"612\" height=\"166\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>4.8 Doubly Linked Lists<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Another important representation of lists is the <strong><em>doubly linked list<\/em><\/strong>which has has<strong><em>two<\/em><\/strong> references in each node; one to the <strong>next<\/strong> element in the list and one to the <strong>previous<\/strong> element. This makes moving back and forth in a list easier, and eliminates the need for a previous reference in particular algorithms. The only disadvantage is that it requires a bit more overhead when managing the list.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A doubly linked list provides a natural implementation of the List ADT. Nodes storedata, link to the previous node and link to the next node (Figure 4.15).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-62 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-15.png\" alt=\"\" width=\"371\" height=\"232\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>Figure 4.15Node of a Doubly Linked List<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>4.8.1 Sentinel Nodes<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In order to simplify implementation and to avoid the empty list to be treated as special cases, two specific nodes have been added at both ends of the doubly-linked list. These are the head and tail which are dummy nodes. These nodes are also called sentinels.They do not store any data elements. The Head or the header sentinel has a <strong><em>null-prev <\/em><\/strong>reference (link) and the Tail or the trailer sentinel has a<strong><em> null-next <\/em><\/strong>reference (link).<\/p>\r\n&nbsp;\r\n\r\nA doubly-linked list in this case canaccessed using the following:\r\n\r\n&nbsp;\r\n\r\n\u2022 Reference to sentinel <strong>head<\/strong>-node;\r\n\r\n\u2022 Reference to sentinel <strong>tail<\/strong>-node; and possibly\r\n\r\n\u2022 A <strong>Size<\/strong>-counter that keeps track of the number of nodes in the list (excluding the\u00a0<span style=\"font-size: 1em;text-align: initial\">two sentinels).<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0<\/strong>\r\n\r\n<strong> 4.8.2 Empty Doubly-Linked List<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Using sentinels, we have no null-links in the actual list nodes. The empty list is as shown in Figure 4.16 where the two sentinels point to each other.<\/p>\r\n&nbsp;\r\n\r\n<strong>head.prev<\/strong>=&gt;<strong>null<\/strong>\r\n\r\n<strong>head.next<\/strong>=&gt;<strong> tail<\/strong>\r\n\r\n<strong>tail.prev<\/strong>=&gt;<strong> head<\/strong>\r\n\r\n<strong>tail.next<\/strong>=&gt;<strong>null<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-63 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-16.png\" alt=\"\" width=\"436\" height=\"178\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>4.8.3 Single Node List<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us see how a doubly linked list with a Single Node will look like.This single node is the first node, and is also the last node. The first node is head.next and is also thetail.prev( Figure 4.17).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-64 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-17.png\" alt=\"\" width=\"417\" height=\"265\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>4.8.3 Advantages over Singly-linked Lists<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Doubly Linked Lists has a number of advantages over singly linked lists. One important advantage is the quick update operationssuch asinsertions, deletions at <em>both<\/em> ends (head and tail), and also in the middle of the list.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 <\/strong>\r\n\r\n<strong>4.8.4 Insertion into Doubly Linked List (DLL)<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe possible cases of inserting a node into a doubly linked list include insertion:\r\n\r\n1. At the front of the Doubly Linked List\r\n\r\n2.After a given node\r\n\r\n3.At the end of the Doubly Linked List\r\n\r\n&nbsp;\r\n\r\n<strong>4.8.4.1 Insertion at the front of Doubly Linked List<\/strong>\r\n\r\n<img class=\"alignnone size-full wp-image-65 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-18.png\" alt=\"\" width=\"654\" height=\"395\" \/>\r\n<p style=\"text-align: justify\">The insertion at the front of a doubly linked list with sentinels is shown in Figure 4.18 and the steps are described in Figure 4.19. Let us assume we call the function:<\/p>\r\n&nbsp;\r\n\r\n<strong>Insertfront(10,List)<\/strong>\r\n\r\n<img class=\"size-full wp-image-744 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-42.png\" alt=\"\" width=\"806\" height=\"403\" \/>\r\n<p style=\"text-align: center\"><strong>Figure 4.19 Steps for Inserting at the Front of a Doubly Linked List<\/strong><\/p>\r\n\r\n<\/div>\r\n<strong style=\"font-size: 1em\">\u00a0 \u00a04.8.4.2 Insertion after the given node in DLL<\/strong>\r\n<div>\r\n<p style=\"text-align: left\">\u00a0 \u00a0Let us assume we call the function:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: left\"><strong>Insertafter(30, 20, List)<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">As is the case with insertion into a list represented as a linked list we need to iteratively search for the node using a search pointer (P) similar to the procedure shown in Figure 4.7. Initially P=&gt;Head&amp; we search until P.data = 20<\/p>\r\n<img class=\"alignnone size-full wp-image-66 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-19.png\" alt=\"\" width=\"661\" height=\"336\" \/>\r\n<p style=\"text-align: center\"><strong>Figure 4.20 Steps for Inserting after a node of a Doubly Linked List<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The insertion after a given node of a doubly linked list with sentinels is shown in Figure 4.20 and the steps are described in Figure 4.21.<\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><strong><img class=\"size-full wp-image-745 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-43.png\" alt=\"\" width=\"807\" height=\"385\" \/><\/strong><\/p>\r\n\r\n<div style=\"text-align: center\"><strong>Figure 4.21 Steps for Inserting After a node of a Doubly Linked List<\/strong><\/div>\r\n<strong>4.8.4.3 Insertion at the end of DLL<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Insertion at the end of a list is achieved without the iterative search for the end of the list if we have the sentinel Tail. The procedure is shown in Figure 4.22 and Figure 4.23 respectively.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-67 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-20.png\" alt=\"\" width=\"675\" height=\"377\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong><img class=\"size-full wp-image-746 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-44.png\" alt=\"\" width=\"807\" height=\"408\" \/><\/strong><\/p>\r\n&nbsp;\r\n\r\n<strong>4.8.5 Deletion from Doubly Linked List<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we want to delete a node P from the doubly linked list and return the data value stored at P. The procedure and the steps for this deletion is given in Figure 4.24 and Figure 4.25 respectively. Here we are remove unnecessary links of P by setting it to null before removing it.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-68 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-21.png\" alt=\"\" width=\"573\" height=\"357\" \/>\r\n<div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-747 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-45.png\" alt=\"\" width=\"785\" height=\"336\" \/><\/p>\r\n<strong>4.9 Performance<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the implementation of the List ADT by means of a doubly linked list the space used by a list with <strong><em>n<\/em><\/strong> elements is <strong><em>O<\/em><\/strong>(<strong><em>n<\/em><\/strong>) and the space used by each position of the list is <strong><em>O<\/em><\/strong>(1). All the operations of the List ADT run in <strong><em>O<\/em><\/strong>(1) time.<\/p>\r\n&nbsp;\r\n\r\n<strong>4.10 Advantages and Disadvantages of Linked List Implementation<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Advantages<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Some of the advantages of linked list implementation include access to any item is possible as long as an external link to first item maintained. Insertion of a new item into the list is possible without any shifting. Similarly deletion from the list does not involve any shifting. It is possible to expand\/contract the list as needed.<\/p>\r\n&nbsp;\r\n\r\n<strong>Disadvantages<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">One of the disadvantages of linked lists is the overhead due to links.Since it is used only internally for the implementation, it is pure overhead. The list adds nodes and removes nodes as and when needed so there is a need to have a facility to provide nodes and remove nodes dynamically. Thereis no longer direct access to each element of the list whereas many sorting algorithms for example binary search need direct access. Access of nth item now less efficient since we must go through first element, and then second, and then third, etc.<\/p>\r\n&nbsp;\r\n\r\n<strong>4.11 Polynomial ADT<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this module we will also discuss the Polynomial ADT. Recall that we have studied about polynomials in mathematics. An example of a single variable polynomial having four terms is 4x<strong>6<\/strong> + 10x<strong>4<\/strong> - 5x + 3 . The order of this polynomial as determined by the highest exponent is 6. Why call it an Abstract Data Type (ADT)? Now let us consider the function f(x) written as a polynomial given below that is a single variable polynomial that can be generalized as:<\/p>\r\n&nbsp;\r\n\r\n<img class=\" wp-image-69 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-22.png\" alt=\"\" width=\"446\" height=\"225\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Notice the two visible data sets namely: (C and E), where C is the coefficient object (Real Number) and E is the exponent object (Integer Number).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">By definition a data type consists of a set of values and a set of allowable operations on those values. Associated with this polynomial the various operations can be add &amp;subtract, multiply, differentiate, integrate, etc\u2026<\/p>\r\n&nbsp;\r\n\r\n<strong>4.12 Analysis of List Implementations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In both array and linked implementations, many operations are similar in efficiency. Most operations are of the orderof <strong>O(1)<\/strong> , except when shifting or searching is required in which case they are of the order of <strong>O(n).<\/strong> In particular situations, the frequency of the need for particular operationsdepending on the application may guide the use of one approach over another.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Discussed the implementation of List ADT using Linked list<\/li>\r\n \t<li>E<span style=\"font-size: 1em\">xplained the different linked list implementations and how the different operations are implemented<\/span><\/li>\r\n \t<li><span style=\"font-size: 1em\">Outlined the Polynomial ADT<\/span><\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Linked List Implementation of List ADT<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/hsp3Nz80jjk\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<img class=\"alignnone wp-image-70 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-23.png\" alt=\"\" width=\"761\" height=\"573\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/hsp3Nz80jjk\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures.This is the fourth module of the course and we will be talking about the linked list implementation of List ADT.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The learning objectives of this module are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 To understand Linked List Implementation of List ADT<\/p>\n<p style=\"text-align: justify\">\u2022 To explore the singly linked list, the circular list and the doubly linked list implementations of List ADT<\/p>\n<p>\u2022 To know about the representation of a polynomial as a linked list<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.1 Linked List Implementation (Pointer)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Sometimes it is not possible or realistic to ensure that the list is stored contiguously as it may not be possible to determine the size of the list required beforehand. We may end up wasting space because we have overestimated the requirements. In such cases we use a linked list. In this case we have a series of structures or locations that are not necessarily adjacent in memory. Compared to the array implementation, the pointer implementation uses only as much space as is needed for the elements currently on the listbut however requires additional space for the pointers in each cell of the linked list.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Linked lists require some additional steps of code during implementation, but however have some specific advantages as the linked list is dynamic as they can easily grow and shrink in size.We don\u2019t need to know how many nodes will be in the list. The locations necessary for the linked list are obtained as when needed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.2 Easy and Fast Insertions and Deletions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To insert or delete an element in an array what is needed is to copy to temporary variables to make room for new elements and close the gap caused by deleted elements.With a linked list, there is no need to move other nodes. We need to only reset some pointers. In other words for a linked list implementation there is no need to estimate the maximum size of the list and consequently there is no wasted space. The time taken for some of the common operations such as printList, find and findKth is of the order of O(n) while insert and delete is of the order of O(1). Insert at position 0 (making a new element) or inserting does not require moving the other elements.<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Similarly delete at position 0 or deleting requires no shifting of elements. Insertion and deletion becomes easier, but finding the Kth element for the place where insertion or deletion needs to be carried out requires time of the order of O(n).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: left\"><strong>4.3 Linked Lists<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A linked listis essentially a series of nodes connected by links where each node contains some data (any type) and apointer to the next node in the list. In some cases there may be a <strong><em>Head<\/em><\/strong>whichis a pointer to the first node. The Head node has null data value with its pointer pointing to the first node in the list. For convenience we do not show the contents of the head node except the pointer to the first node in the list.The last node of a linked list normally points to NULL(Figure 4.1). Note, that the nodes in a linked list can be <strong>spread out <\/strong>over the memory and need not occupy contiguous memory locations. In all the discussions regarding insertion and deletion operations on lists we assume that the list is not empty and hence we do not discuss boundary conditions.<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-55 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-8.png\" alt=\"\" width=\"699\" height=\"257\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-8.png 699w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-8-300x110.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-8-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-8-225x83.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-8-350x129.png 350w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><\/p>\n<p style=\"text-align: center\"><strong>4.3 Insertion- The Scenario<\/strong><\/p>\n<p style=\"text-align: justify\">Let us study the insertion scenario in the case of linked lists. You have a linked list which may be empty or not, may beordered, or not. You want to add an element into the given linked list (Figure 4.2).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-56 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-9.png\" alt=\"\" width=\"639\" height=\"134\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-9.png 639w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-9-300x63.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-9-65x14.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-9-225x47.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-9-350x73.png 350w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/p>\n<p style=\"text-align: justify\">When inserting a new node into a linked list, there are four possible cases as described below:<\/p>\n<p>&nbsp;<\/p>\n<p>1. Insert into an empty list<\/p>\n<p>2.Insert in front<\/p>\n<p>3. Insert at back<\/p>\n<p>4. Insert in middle<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">However in fact, we need to handle only two cases. The insert as the first node can handle Case 1 and Case 2. Insert in the middle or at the end of the list Case 3 and Case\u00a0<span style=\"font-size: 1em;text-align: initial\">4can be handled in a similar manner.<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0<\/strong><\/p>\n<p><strong> 4.3.1 The Actual Insertion<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The actual insertion into a linked list involves the following steps:<\/p>\n<ul>\n<li style=\"text-align: justify\">Creation of a new node \u2013We use a function Freenode that gives a node in the structure required to be used by the linked list .<\/li>\n<li style=\"text-align: justify\">Adding the data \u2013 We add the data to the data component of the node provided by Freenode.<\/li>\n<li style=\"text-align: justify\">Updating the next pointer of the new node \u2013 We update the Next pointer (pointer pointing to next node in the linked list) of the new node just obtained to point to the appropriate node.<\/li>\n<li style=\"text-align: justify\">Update current to point to new node \u2013 Now we update the current node\u2019s (obtained through appropriate search procedure) next pointer to point to the new node.<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 \u00a0 \u00a0 4.3.2 Inserting to the Empty Or to the Front of a Linked List<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-57 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-10.png\" alt=\"\" width=\"597\" height=\"211\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-10.png 597w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-10-300x106.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-10-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-10-225x80.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-10-350x124.png 350w\" sizes=\"auto, (max-width: 597px) 100vw, 597px\" \/><\/p>\n<p><span style=\"text-align: initial;text-indent: 1em;font-size: 1em\">Let us assume that we call inserting into the front of the list as follows:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Insertfront (93, List)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this case there is no work to find the correct location. Empty or not, head will point to the right location. For the list shown in Figure 4.3, the following are the steps (Figure 4.4)<\/p>\n<\/div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-737 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-35.png\" alt=\"\" width=\"795\" height=\"248\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-35.png 795w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-35-300x94.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-35-768x240.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-35-65x20.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-35-225x70.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-35-350x109.png 350w\" sizes=\"auto, (max-width: 795px) 100vw, 795px\" \/><\/p>\n<div><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a04.3.3 Inserting to the End of a List<\/strong><\/div>\n<div>\n<p>\u00a0 \u00a0 Let us assume that we call inserting to the end of the list as follows:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Insertend (83, List)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this case there is a need to find the correct location for the insertion to take place. We need to know the address of the current last node in order to connect to the new last node. We needto perform iteration (Figure 4.5) in order to find the address of current last node for which we need a search pointer (P).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-738 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-36.png\" alt=\"\" width=\"775\" height=\"225\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-36.png 775w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-36-300x87.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-36-768x223.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-36-65x19.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-36-225x65.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-36-350x102.png 350w\" sizes=\"auto, (max-width: 775px) 100vw, 775px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we need to insert a node after the Node P which we have found through the iterative procedure. The steps are shown in Figure 4.6.<\/p>\n<\/div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-739 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-37.png\" alt=\"\" width=\"553\" height=\"206\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-37.png 430w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-37-300x112.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-37-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-37-225x84.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-37-350x130.png 350w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a04.3.4 Inserting into the Middle of Unsorted List &amp; Sorted List<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this type of insertion also we need to traverse the list recursively until we find the correct place to insert either after a particular given data value or insert at the correct place in a sorted list.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.3.4.1Inserting in the Middle<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The case we will first consider is inserting in the middle of an unsorted list iswhere we insert in the middle of the list because we have found the node containing a data value (17) before which we want to insert the given data (88).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Insertbefore (88,17,List)<\/strong><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">As discussed previously, the operation insert takes us to the iterative procedure for finding the position after which we want to insert or the data value before which we want to insert. Note here that we need the address of the node before the node containing 17 in order to carry out the insertion. This finding of appropriate position is a common need (Figure 4.7). After finding the position P, the node after which we want to insert, we carry out the insertion (Figure 4.8and Figure 4.9)<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-740 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-38.png\" alt=\"\" width=\"806\" height=\"245\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-38.png 806w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-38-300x91.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-38-768x233.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-38-65x20.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-38-225x68.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-38-350x106.png 350w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 4.7 Steps for Iteratively Finding a Data Item<\/strong><\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-741 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-39.png\" alt=\"\" width=\"628\" height=\"228\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-39.png 435w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-39-300x109.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-39-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-39-225x82.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-39-350x127.png 350w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-58 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-11.png\" alt=\"\" width=\"651\" height=\"369\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-11.png 651w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-11-300x170.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-11-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-11-225x128.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-11-350x198.png 350w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-59 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-12.png\" alt=\"\" width=\"615\" height=\"184\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-12.png 615w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-12-300x90.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-12-65x19.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-12-225x67.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-12-350x105.png 350w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<p style=\"text-align: justify\">The next case of inserting in the middle of a list is insert a data item into a given sorted list (Figure 4.10) by using the following:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Insertsorted(50,List)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For this we need to find the node after which we want to insert the new data so that the sorted order is maintained. Again a iterative procedure where we need to start from Head using a search variable P until we find the address of the node after which we need to insert \u2013 for this we need to ensure that the node after the one we find has value greater than 50 (Figure 4.11). Here we assume the list is in ascending order.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-742 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-40.png\" alt=\"\" width=\"808\" height=\"312\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-40.png 808w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-40-300x116.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-40-768x297.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-40-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-40-225x87.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-40-350x135.png 350w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/p>\n<p style=\"text-align: justify\">Once we have found the location P after which to insert by using the steps indicated in Figure 4.11then the procedure for insertion is similar to insertion in the middle of an unsorted list (Figure 4.9)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.4 Deletion-The Scenario<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For deletion, we begin with an existing linked list that could be empty or not and could be ordered or not. In the case of deletion there are three possible situations:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Delete the first element<\/p>\n<p>\u2022 Delete the first occurrence of an element<\/p>\n<p>\u2022 Delete all occurrences of a particular element<\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 4.5 The Actual Deletion<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>Deletion involves the following basic steps:<\/p>\n<p>\u2022 Getting to the correct position \u2013 iteratively finding the correct node to delete<\/p>\n<p style=\"text-align: justify\">\u2022 Moving a pointer so nothing points to the element to be deleted (Figure 4.12 and Figure 4.13)<\/p>\n<p>&nbsp;<\/p>\n<p>We want to <strong>Delete(40, Head)<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-60 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-13.png\" alt=\"\" width=\"628\" height=\"306\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-13.png 628w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-13-300x146.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-13-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-13-225x110.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-13-350x171.png 350w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-743 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-41.png\" alt=\"\" width=\"789\" height=\"255\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-41.png 789w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-41-300x97.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-41-768x248.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-41-65x21.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-41-225x73.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-41-350x113.png 350w\" sizes=\"auto, (max-width: 789px) 100vw, 789px\" \/><\/p>\n<p><strong>\u00a0 \u00a0 4.6 Linked List Traversal &amp; Printing<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In general, traversal means \u201cvisiting\u201d or examining each node. In the case of a singly linked list we generally start at the beginning of the list and go one node at a time until the end. This is a recursive procedure (function) that given a head pointer, looks at just one node at a time.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.7 Circular Linked Lists<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Another representation of the list is a variation of the singly linked list called the circular linked list (Figure 4.14) where the last node points to the first node of the list. One question associated with circular linked lists is how do we know when we have finished traversing the list? In order to solve this problem we need to check if the pointer of the current node is equal to the head. We may need to cycle through a list repeatedlye.g.<\/p>\n<\/div>\n<div>\n<p>when we use the list to implement a round robin system for a shared resource. In this case having the last node point to the first node is a solution.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-61 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-14.png\" alt=\"\" width=\"612\" height=\"166\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-14.png 612w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-14-300x81.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-14-65x18.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-14-225x61.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-14-350x95.png 350w\" sizes=\"auto, (max-width: 612px) 100vw, 612px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.8 Doubly Linked Lists<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Another important representation of lists is the <strong><em>doubly linked list<\/em><\/strong>which has has<strong><em>two<\/em><\/strong> references in each node; one to the <strong>next<\/strong> element in the list and one to the <strong>previous<\/strong> element. This makes moving back and forth in a list easier, and eliminates the need for a previous reference in particular algorithms. The only disadvantage is that it requires a bit more overhead when managing the list.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A doubly linked list provides a natural implementation of the List ADT. Nodes storedata, link to the previous node and link to the next node (Figure 4.15).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-62 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-15.png\" alt=\"\" width=\"371\" height=\"232\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-15.png 371w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-15-300x188.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-15-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-15-225x141.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-15-350x219.png 350w\" sizes=\"auto, (max-width: 371px) 100vw, 371px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Figure 4.15Node of a Doubly Linked List<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.8.1 Sentinel Nodes<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In order to simplify implementation and to avoid the empty list to be treated as special cases, two specific nodes have been added at both ends of the doubly-linked list. These are the head and tail which are dummy nodes. These nodes are also called sentinels.They do not store any data elements. The Head or the header sentinel has a <strong><em>null-prev <\/em><\/strong>reference (link) and the Tail or the trailer sentinel has a<strong><em> null-next <\/em><\/strong>reference (link).<\/p>\n<p>&nbsp;<\/p>\n<p>A doubly-linked list in this case canaccessed using the following:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Reference to sentinel <strong>head<\/strong>-node;<\/p>\n<p>\u2022 Reference to sentinel <strong>tail<\/strong>-node; and possibly<\/p>\n<p>\u2022 A <strong>Size<\/strong>-counter that keeps track of the number of nodes in the list (excluding the\u00a0<span style=\"font-size: 1em;text-align: initial\">two sentinels).<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0<\/strong><\/p>\n<p><strong> 4.8.2 Empty Doubly-Linked List<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Using sentinels, we have no null-links in the actual list nodes. The empty list is as shown in Figure 4.16 where the two sentinels point to each other.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>head.prev<\/strong>=&gt;<strong>null<\/strong><\/p>\n<p><strong>head.next<\/strong>=&gt;<strong> tail<\/strong><\/p>\n<p><strong>tail.prev<\/strong>=&gt;<strong> head<\/strong><\/p>\n<p><strong>tail.next<\/strong>=&gt;<strong>null<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-63 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-16.png\" alt=\"\" width=\"436\" height=\"178\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-16.png 436w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-16-300x122.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-16-65x27.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-16-225x92.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-16-350x143.png 350w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.8.3 Single Node List<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us see how a doubly linked list with a Single Node will look like.This single node is the first node, and is also the last node. The first node is head.next and is also thetail.prev( Figure 4.17).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-64 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-17.png\" alt=\"\" width=\"417\" height=\"265\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-17.png 417w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-17-300x191.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-17-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-17-225x143.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-17-350x222.png 350w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.8.3 Advantages over Singly-linked Lists<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Doubly Linked Lists has a number of advantages over singly linked lists. One important advantage is the quick update operationssuch asinsertions, deletions at <em>both<\/em> ends (head and tail), and also in the middle of the list.<\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 <\/strong><\/p>\n<p><strong>4.8.4 Insertion into Doubly Linked List (DLL)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The possible cases of inserting a node into a doubly linked list include insertion:<\/p>\n<p>1. At the front of the Doubly Linked List<\/p>\n<p>2.After a given node<\/p>\n<p>3.At the end of the Doubly Linked List<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.8.4.1 Insertion at the front of Doubly Linked List<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-65 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-18.png\" alt=\"\" width=\"654\" height=\"395\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-18.png 654w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-18-300x181.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-18-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-18-225x136.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-18-350x211.png 350w\" sizes=\"auto, (max-width: 654px) 100vw, 654px\" \/><\/p>\n<p style=\"text-align: justify\">The insertion at the front of a doubly linked list with sentinels is shown in Figure 4.18 and the steps are described in Figure 4.19. Let us assume we call the function:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Insertfront(10,List)<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-744 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-42.png\" alt=\"\" width=\"806\" height=\"403\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-42.png 806w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-42-300x150.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-42-768x384.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-42-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-42-225x113.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-42-350x175.png 350w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><\/p>\n<p style=\"text-align: center\"><strong>Figure 4.19 Steps for Inserting at the Front of a Doubly Linked List<\/strong><\/p>\n<\/div>\n<p><strong style=\"font-size: 1em\">\u00a0 \u00a04.8.4.2 Insertion after the given node in DLL<\/strong><\/p>\n<div>\n<p style=\"text-align: left\">\u00a0 \u00a0Let us assume we call the function:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: left\"><strong>Insertafter(30, 20, List)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As is the case with insertion into a list represented as a linked list we need to iteratively search for the node using a search pointer (P) similar to the procedure shown in Figure 4.7. Initially P=&gt;Head&amp; we search until P.data = 20<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-66 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-19.png\" alt=\"\" width=\"661\" height=\"336\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-19.png 661w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-19-300x152.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-19-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-19-225x114.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-19-350x178.png 350w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/p>\n<p style=\"text-align: center\"><strong>Figure 4.20 Steps for Inserting after a node of a Doubly Linked List<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The insertion after a given node of a doubly linked list with sentinels is shown in Figure 4.20 and the steps are described in Figure 4.21.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-745 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-43.png\" alt=\"\" width=\"807\" height=\"385\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-43.png 807w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-43-300x143.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-43-768x366.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-43-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-43-225x107.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-43-350x167.png 350w\" sizes=\"auto, (max-width: 807px) 100vw, 807px\" \/><\/strong><\/p>\n<div style=\"text-align: center\"><strong>Figure 4.21 Steps for Inserting After a node of a Doubly Linked List<\/strong><\/div>\n<p><strong>4.8.4.3 Insertion at the end of DLL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Insertion at the end of a list is achieved without the iterative search for the end of the list if we have the sentinel Tail. The procedure is shown in Figure 4.22 and Figure 4.23 respectively.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-67 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-20.png\" alt=\"\" width=\"675\" height=\"377\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-20.png 675w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-20-300x168.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-20-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-20-225x126.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-20-350x195.png 350w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-746 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-44.png\" alt=\"\" width=\"807\" height=\"408\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-44.png 807w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-44-300x152.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-44-768x388.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-44-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-44-225x114.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-44-350x177.png 350w\" sizes=\"auto, (max-width: 807px) 100vw, 807px\" \/><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.8.5 Deletion from Doubly Linked List<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we want to delete a node P from the doubly linked list and return the data value stored at P. The procedure and the steps for this deletion is given in Figure 4.24 and Figure 4.25 respectively. Here we are remove unnecessary links of P by setting it to null before removing it.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-68 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-21.png\" alt=\"\" width=\"573\" height=\"357\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-21.png 573w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-21-300x187.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-21-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-21-225x140.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-21-350x218.png 350w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-747 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-45.png\" alt=\"\" width=\"785\" height=\"336\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-45.png 785w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-45-300x128.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-45-768x329.png 768w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-45-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-45-225x96.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-45-350x150.png 350w\" sizes=\"auto, (max-width: 785px) 100vw, 785px\" \/><\/p>\n<p><strong>4.9 Performance<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the implementation of the List ADT by means of a doubly linked list the space used by a list with <strong><em>n<\/em><\/strong> elements is <strong><em>O<\/em><\/strong>(<strong><em>n<\/em><\/strong>) and the space used by each position of the list is <strong><em>O<\/em><\/strong>(1). All the operations of the List ADT run in <strong><em>O<\/em><\/strong>(1) time.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.10 Advantages and Disadvantages of Linked List Implementation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Advantages<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Some of the advantages of linked list implementation include access to any item is possible as long as an external link to first item maintained. Insertion of a new item into the list is possible without any shifting. Similarly deletion from the list does not involve any shifting. It is possible to expand\/contract the list as needed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Disadvantages<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">One of the disadvantages of linked lists is the overhead due to links.Since it is used only internally for the implementation, it is pure overhead. The list adds nodes and removes nodes as and when needed so there is a need to have a facility to provide nodes and remove nodes dynamically. Thereis no longer direct access to each element of the list whereas many sorting algorithms for example binary search need direct access. Access of nth item now less efficient since we must go through first element, and then second, and then third, etc.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.11 Polynomial ADT<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this module we will also discuss the Polynomial ADT. Recall that we have studied about polynomials in mathematics. An example of a single variable polynomial having four terms is 4x<strong>6<\/strong> + 10x<strong>4<\/strong> &#8211; 5x + 3 . The order of this polynomial as determined by the highest exponent is 6. Why call it an Abstract Data Type (ADT)? Now let us consider the function f(x) written as a polynomial given below that is a single variable polynomial that can be generalized as:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-69 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-22.png\" alt=\"\" width=\"446\" height=\"225\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-22.png 284w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-22-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-22-225x113.png 225w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Notice the two visible data sets namely: (C and E), where C is the coefficient object (Real Number) and E is the exponent object (Integer Number).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">By definition a data type consists of a set of values and a set of allowable operations on those values. Associated with this polynomial the various operations can be add &amp;subtract, multiply, differentiate, integrate, etc\u2026<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4.12 Analysis of List Implementations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In both array and linked implementations, many operations are similar in efficiency. Most operations are of the orderof <strong>O(1)<\/strong> , except when shifting or searching is required in which case they are of the order of <strong>O(n).<\/strong> In particular situations, the frequency of the need for particular operationsdepending on the application may guide the use of one approach over another.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Discussed the implementation of List ADT using Linked list<\/li>\n<li>E<span style=\"font-size: 1em\">xplained the different linked list implementations and how the different operations are implemented<\/span><\/li>\n<li><span style=\"font-size: 1em\">Outlined the Polynomial ADT<\/span><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Linked List Implementation of List ADT<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/hsp3Nz80jjk\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-70 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-23.png\" alt=\"\" width=\"761\" height=\"573\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-23.png 647w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-23-300x226.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-23-65x49.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-23-225x169.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-23-350x263.png 350w\" sizes=\"auto, (max-width: 761px) 100vw, 761px\" \/><\/p>\n","protected":false},"author":3,"menu_order":4,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-t-v-geetha"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-52","chapter","type-chapter","status-publish","hentry","contributor-dr-t-v-geetha"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":27,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/52\/revisions"}],"predecessor-version":[{"id":899,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/52\/revisions\/899"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/52\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=52"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=52"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}