{"id":40,"date":"2018-07-18T08:39:52","date_gmt":"2018-07-18T08:39:52","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=40"},"modified":"2018-12-12T07:06:50","modified_gmt":"2018-12-12T07:06:50","slug":"array-implementation-of-list-adt","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/array-implementation-of-list-adt\/","title":{"rendered":"Array Implementation of List ADT"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/HPPGQ9wj0no\" 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 third module of the paper and we will be talking about the array 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 discuss the different implementations of List ADT\r\n\r\n\u2022 To understand the array and array based vector implementation of Lists\r\n\r\n\u2022 To outline the implementation of Lists using dynamic arrays\r\n\r\n&nbsp;\r\n\r\n<strong>3.1<\/strong>\u00a0<strong>Implementation of an ADT<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Before we go on to describe the implementation of a particular type of ADT let us discuss the general issues in implementing an ADT. The first step in the implementation is the choiceof the data structure to represent the ADT\u2019s data. This choice of the data structure depends mainly on details of the ADT\u2019s operations and the context in which the operations will be used. You must remember that the implementation details should be hidden behind a wall of ADT operations. In other words a program would only be able to access the data structure using the ADT operations.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The first step in implementing an ADT is choosing a <strong>data structure<\/strong>such as arrays, records, etc. to represent the ADT. Each operation associated with the ADT is implemented by one or more subroutines. Two standard implementations for the list ADT that we will be discussing in this paper are array-based implementation and linked list based implementation. In this module we will be discussing the array based implementation of the List ADT.<\/p>\r\n&nbsp;\r\n\r\n<strong>3.2 Static Implementation of ADT List<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The simplest method to implement a List ADT is to use an array that is a\u201clinear list\u201d or a \u201ccontiguous list\u201d where elements are stored in contiguous array positions. The implementation specifies an array of a particular maximum length, and all storage is allocated before run-time. It is a sequence of n-elements where the items in the array are stored with the index of the array related to the position of the item in the list.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In array implementation,elements are stored in contiguous array positions (Figure 3.1). An array is a viable choice for storing list elements when the elements are sequential,\u00a0<span style=\"font-size: 1em;text-align: initial\">because it is a commonly available data type and in general algorithm development is easy.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><img class=\"alignnone size-full wp-image-43 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled.png\" alt=\"\" width=\"409\" height=\"253\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>3.2.1List Implementation using arrays<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In order to implement lists using arrays we need an array for storing entries \u2013 <strong>listArray[0,1,2\u2026..m], <\/strong>a variable<strong> curr<\/strong>to keep track of the number of array elements currently assigned to the list<strong>,<\/strong> thenumber of items in the list, or current size of the list <strong>size<\/strong>and a variable to record the maximum length of the array, and therefore of the list \u2013<strong> maxsize as shown in Figure 3.2.<\/strong><\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-44 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-1.png\" alt=\"\" width=\"604\" height=\"171\" \/>\r\n<p style=\"text-align: justify\">Fix one end of the list at index 0 and elements will be shifted<strong><em>as needed<\/em><\/strong> when an element is added or removed. Therefore insert and delete operations will take O(n). That is if we want to insert at position 0 (insert as first element),this requires first pushing the entire array to the right one spot to make room for the new element. Similarly deleting the element at position 0requires shifting all the elements in the list left one spot.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">One of the important operations associated with lists is the initialization. In this case we need to first declare the array of list items with maximum number of items say n. This then requires an estimate of the maximum size of the list. Once we decide the size, then we `initialize the list with number of items set at 0.<\/p>\r\n&nbsp;\r\n\r\n<strong>3.2.1.1 Finding First Element<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here we define finding the first element by first calling a Boolean function \u2013 IsEmpty list. If IsEmpty listis true that is if the list is empty then there is no question of finding an element and finding the first element will return a value that is not a valid entry of the list However if IsEmpty listis false that is the list is not empty the element at the first location position (0) is returned.<\/p>\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a03.2.1.2 Insertion<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">What happens if you want to insert an item at a specified position in an existing array?The item originally at the given index must be moved right one position, and all the items after that index must be <strong><em>shifted right<\/em><\/strong>(Figure 3.3).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-45 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-2.png\" alt=\"\" width=\"571\" height=\"231\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us consider a specific case of Insert operation. Insert (3, K, List) In this case, 3 is the index or the point of Insertion, K is the element to be inserted &amp; List is the list in which insertion is to be done ( Figure 3.4 (a) &amp; Figure 3.4 (b)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-46 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-3.png\" alt=\"\" width=\"625\" height=\"417\" \/>\r\n<p style=\"text-align: justify\">Let us see the steps in the insertion with the example given in Figure 3.4(a) &amp; Figure 3.4 (b). Let us assume initially that the original list contains 7 elements (that is Size =7) namely A at array location 0, X at location 1 and so on and finally Rat location 6. The steps involved are as follows:<\/p>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">Step 1: First check whether there is place to add the new element \u2013 in other words whether the array is full. If so indicate error<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>size = maxsize \u2013 Then error<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 2: Now we need to make room for the new element at the index position 3. We need to shift elements from index to curr (curr position of last element of list) one position to the right.<\/p>\r\n&nbsp;\r\n\r\n<strong>Loop till curr<\/strong>\r\n\r\n<strong>Items[index+1] =&gt;<\/strong><strong>tems[index]<\/strong>\r\n\r\n&nbsp;\r\n\r\nStep 3: Write the element into the gap created by shifting the elements to the right\r\n\r\n<strong>Items[index] <\/strong><strong>=&gt;<\/strong><strong> K<\/strong>\r\n\r\n&nbsp;\r\n\r\nStep 4: Update size and curr position of the list\r\n\r\n<strong>size=&gt;<\/strong><strong> size +1; curr<\/strong><strong>=&gt;<\/strong><strong>curr +1<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>3.2.2.2 Deletion from Lists<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">What happens if you want to remove an item from a specified position in an existing array? There are two ways in which this can be done:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2013\u00a0\u00a0 Leave <strong><em>gaps<\/em><\/strong> in the array, i.e. indices that contain no elements, which in practice, means that the array element has to be given a special value to indicate that it is \u201cempty\u201d, or<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2013 All the items after the (removed item\u2019s) index must be <strong><em>shifted left<\/em><\/strong>similar to what we did when we wanted to insert \u2013 only for insertion we shifted right.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2013 <strong>Delete (3, List)<\/strong>. In this case, 3 is the index or the point of Deletion&amp; List is the list from which deletion is to be done (Figure 3.5 (a) &amp; Figure 3.5 (b)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-47 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-4.png\" alt=\"\" width=\"608\" height=\"416\" \/>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">Let us see the steps in the deletion with the example given in Figure 3.5(a) &amp; Figure 3.5(b). Let us assume initially that the original list contains 7 elements (that is Size =7) namely A at array location 0, X at location 1 and so on and finally R at location 6. The steps involved are as follows:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 1: First check whether the list is empty that is whether there are elements in the list.<\/p>\r\nWe cannot perform deletion from an empty list.\r\n\r\n<strong>size=0 Then error<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 2: Now we need to shift elements from index position 4 to the left so that an empty locationis created due to the deletion at location 3 will be filled by elements to the left. We need to shift elements from index (i) to curr( that is the curr position of last element of list) one position to the left.<\/p>\r\n&nbsp;\r\n\r\n<strong>Loop till curr<\/strong>\r\n\r\n<strong>Items[index] <\/strong><strong>=&gt;<\/strong><strong> items[index+1]<\/strong>\r\n\r\n&nbsp;\r\n\r\nStep 4: Update size and curr position of the list\r\n\r\n<strong>size=&gt;<\/strong><strong>size-1; curr<\/strong><strong>=&gt;<\/strong><strong>curr-1<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>3.2 Advantages of Array-Based Implementation of Lists<\/strong>\r\n\r\n&nbsp;\r\n\r\nSome of the major advantages of using array implementation of lists are:\r\n<ul>\r\n \t<li>Array is a natural way to implement lists<\/li>\r\n \t<li>Arrays allow fast, random access of elements<\/li>\r\n \t<li style=\"text-align: justify\">Array based implementation is memory efficient since very little memory is required other than that needed to store the actual contents<\/li>\r\n<\/ul>\r\nSome of the disadvantages of using arrays to implement lists are:\r\n<ul>\r\n \t<li style=\"text-align: justify\">The size of the list must be known when the array is created and is fixed (static)<\/li>\r\n \t<li style=\"text-align: justify\">Array implementations of lists use a static data structure. Often defined at compile-time. This means the array size or structure cannot be altered while program is running. This requires an accurate estimate of the size of the array.<\/li>\r\n \t<li style=\"text-align: justify\">This fixing of the size beforehand usually results in overestimation of size which means we tend to usually waste space rather than have program run out.<\/li>\r\n \t<li style=\"text-align: justify\">The deletion and insertion of elements into the list is slow since it involves shifting of elements. It also means that data must be added to the end of the list for insertion and deletion to be efficient. If insertion and deletion is towards the front of the list, all other elements must shuffle down. This is slow and inefficient. This inefficiency is even more pronounced when the size of the list is large.<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 \u00a03.3 The Vector ADT<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Vector ADT extends the notion of array by storing a sequence of arbitrary objects. However instead of using position or index to access an element, an element can be accessed, inserted or removed by specifying its rank that is the number of elements preceding it. An exception will be thrown if an incorrect rank is specified (e.g. negative rank, or larger than current size). The main vector based list operations are as follows:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022\u00a0 elemAtRank(integer r): This operation returns the element at rank r without removing it.<\/p>\r\n<p style=\"text-align: justify\">\u2022 replaceAtRank(integer r, object o): This operation replaces the element at\u00a0<span style=\"font-size: 1em;text-align: initial\">rank with an element o and returns the old element.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 insertAtRank(integer r, object o): This operation inserts a new element o so that it has rank r<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">\u2022 removeAtRank(integer r): This operation removes and returns the element at rank r Other common operations includesize() and isEmpty().<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0<\/strong>\r\n\r\n<strong>3.3.1\u00a0 Applications of Vectors<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Vectors have some specific applications. Direct applications include sorted collection of objects like in an elementary database. There are also some indirect applications such as use as an auxiliary data structure for algorithms and as components of other data structures.<\/p>\r\n&nbsp;\r\n\r\n<strong>3.3.2 Array-based Vector<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this implementation we use an array <strong><em>V<\/em><\/strong>containing <strong><em>n<\/em><\/strong><em>elements<strong>.<\/strong><\/em> A variable <strong><em>size=n<\/em><\/strong> is used to keep track of the size of the vector (number of elements stored). Normally an array is addressed by its index and when representing the list by an array the rank and index are identical. However we differentiate the vector representation with rank to explain the important operations to be supported when using a vector.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The important operation <strong><em>elemAtRank<\/em><\/strong>(<strong><em>r<\/em><\/strong>) is implemented in <strong><em>O<\/em><\/strong>(1) time by returning <strong><em>V<\/em><\/strong>[<strong><em>r<\/em><\/strong>] (Figure 3.6). Similar to array implementation of lists, in operation <strong><em>insertAtRank<\/em><\/strong>(<strong><em>r<\/em>,<\/strong> <strong><em>o<\/em><\/strong>), we need tomake room for the new element by shifting forward the <strong><em>n<\/em><\/strong> - <strong><em>r<\/em><\/strong> elements <strong><em>V<\/em><\/strong>[<strong><em>r<\/em><\/strong>], \u2026,<strong><em>V<\/em><\/strong>[<strong><em>n<\/em><\/strong>-1] (Figure 3.7). In the worst case (<strong><em>r<\/em><\/strong> =0), this takes <strong><em>O<\/em><\/strong>(<strong><em>m<\/em><\/strong>) time<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-48 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-5.png\" alt=\"\" width=\"636\" height=\"340\" \/>\r\n<p style=\"text-align: justify\">Similar to array implementation of lists, in operation <strong><em>removeAtRank<\/em><\/strong>(<strong><em>r<\/em><\/strong>), we need to fill the\u00a0<span style=\"text-align: initial;font-size: 1em\">hole left by the removed element by shifting backward the <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>n<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> - <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>r<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> -1 elements <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>V<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">[<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>r<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> +1], \u2026,<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>V<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">[<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>n<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> -1] ( Figure 3.8). Again in the worst case (<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>r<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> =0), this takes <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>O<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>n<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) time.<\/span><\/p>\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-49 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-6.png\" alt=\"\" width=\"608\" height=\"186\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>3.3 Performance<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the array based implementation of a Vector, the space used by the data structure is <strong><em>O<\/em><\/strong>(<strong><em>n<\/em><\/strong>). The operations<strong><em> size<\/em><\/strong>,<strong><em> isEmpty<\/em><\/strong>,<strong><em> elemAtRank <\/em><\/strong>and<strong><em> replaceAtRank<\/em><\/strong>all run in<strong><em> O<\/em><\/strong>(1) time. However the operations <strong><em>insertAtRank<\/em><\/strong> and <strong><em>removeAtRank<\/em><\/strong> run in <strong><em>O<\/em><\/strong>(<strong><em>n<\/em><\/strong>) time. If we use the array in a circular fashion,<strong><em>insertAtRank<\/em><\/strong>(0) and <strong><em>removeAtRank<\/em><\/strong>(0) run in <strong><em>O<\/em><\/strong>(1) time. In an <strong><em>insertAtRank<\/em><\/strong> operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one<\/p>\r\n&nbsp;\r\n\r\n<strong>3.4 ADT List using Dynamic arrays<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A dynamic data structure is one that changes size, as needed, as items are inserted or removed. There is usually no limit on the size of such structures, other than the size of main memory. Dynamic arrays are arrays that grow (or shrink) as required. In other words a new array is created when the old array becomes full by creating a new array object, copying the values from the old array and then assigning the new array to the existing array reference variable<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-50 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-7.png\" alt=\"\" width=\"681\" height=\"458\" \/>\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">Let us see the steps in the insertion of an element into a dynamic array with the example given in Figure 3.9(a) &amp; Figure 3.9(b). Let us assume initially that the original array has a size of 6 ( 0,1 \u2026.5) and contains 4 elements <\/span><strong style=\"text-align: justify;font-size: 1em\">6,1,7,8<\/strong><span style=\"text-align: justify;font-size: 1em\"> occupying locations 0,1,2 and 3 respectively, with curr pointing to 4 the next empty location (Figure 3.9(a) \u2013 (i)). The steps involved are as follows:<\/span><\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 Step 1: Now let us assume that we want to insert <strong>3<\/strong>. Now element 3 will be inserted in location 4 and curr will be incremented to 5 (Figure 3.9(a) \u2013 (ii)).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 2:Now let us assume that we want to insert <strong>9<\/strong>. Now element 9 will be inserted in location 5 and curr will be incremented to 6 (Figure 3.9(a) \u2013 (iii)).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 3: Now let us assume that we want to insert <strong>2<\/strong>. At this point curr&gt;maxsize of array and list full condition will be signaled as there is no room for a new item. In static array \u2013 an exception will arise.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 4: However in the case of dynamic arrays, we will create a new, bigger array as shown in Figure 3.9(b) \u2013 (i).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 5: Now all the elements of the old array will be copied to the new array and curr set to 6 (Figure 3.9(b) \u2013 (ii)).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Step 6: Now finally we can insert 2 into the new array and accordingly curr is set to 7 (Figure 3.9(b) \u2013 (iii)).<\/p>\r\n&nbsp;\r\n\r\nThe old array will eventually be deleted\r\n\r\n&nbsp;\r\n\r\n<strong>3.4.2 Problems associated with Dynamic Array<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Insertion using dynamic array essentially means that before every insertion, we need to check if the array needs to grow. An important question that arises is that when growing an array, how much to grow it?. Is this decision based on memory efficiency that is grow by one only \u2013 since we need one location when one element is to be inserted? But in this case the time efficiency is very poor since the expensive copy operation has to be carried out every time an insertion occurs. Growing by doubling works well in practice, because it grows very large very quickly -10, 20, 40, 80, 160, 320, 640, 1280,\u2026and so on.In this case very few array re-sizingsare needed. While the copying operation in this case is again expensive, this copying does not have to be done often.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">When the doubling does happen it may be time-consumingbutright after a doubling half the array is empty. However re-sizing after each insertion would be prohibitively slow. Deleting and inserting in the middle of an array (on average) is still of the order of O(n).<\/p>\r\n&nbsp;\r\n\r\n<strong>3.4\u00a0 Summary<\/strong>\r\n<ul>\r\n \t<li>Explained the different implementations of Lists using arrays<\/li>\r\n \t<li>Discussed the Pros and Cons of each implementation<\/li>\r\n \t<li>Discussed the different operations possible with array based implementation of Lists<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Array Implementation of List ADT<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/HPPGQ9wj0no\" 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<strong style=\"font-size: 1em\">\u00a0 \u00a0 \u00a0 Web Links<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\">en.wikipedia.org\/wiki\/List_(abstract_data_type)<\/li>\r\n \t<li style=\"text-align: justify\">www.cs.cmu.edu\/~tcortina\/15110sp12\/Unit06PtA.pdf<\/li>\r\n \t<li style=\"text-align: justify\">www.csee.umbc.edu\/courses\/undergraduate\/341\/...\/Lists\/List1.html<\/li>\r\n \t<li style=\"text-align: justify\">www.doc.ic.ac.uk\/~ar3\/lectures\/ProgrammingII\/...\/Lecture2PrintOut.pdf<\/li>\r\n \t<li style=\"text-align: justify\">www.cs.sfu.ca\/CourseCentral\/225\/...notes\/dynamic-arrays-link_list.pdf<\/li>\r\n \t<li style=\"text-align: justify\">www.math.bas.bg\/~nkirov\/2009\/NETB201\/slides\/ch05\/ch05.html<\/li>\r\n \t<li style=\"text-align: justify\">www.cs.sfu.ca\/CourseCentral\/225\/amhunter\/lecturenotes\/DynamicArray.ppt<\/li>\r\n \t<li style=\"text-align: justify\">http:\/\/orion.lcg.ufrj.br\/Dr.Dobbs\/books\/book3\/chap3.htm<\/li>\r\n \t<li style=\"text-align: justify\">cs.txstate.edu\/~rp44\/cs3358_092\/Lectures\/list_revised.ppt<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>Supporting &amp; Reference Materials<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Carrano and Henry, \u201cData Structures and Problem Solving with C++: Walls and Mirrors\u201d, Pearson; 6 edition, 2012\r\n2. Mark Allen Weiss, \u201cData Structures and Algorithm Analysis in Java\u201d, Pearson; 3rd Edition, 2011\r\n3. Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser, \u201cData Structures and Algorithms in Java\u201d, Wiley; 6 edition, 2014<\/p>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/HPPGQ9wj0no\" 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 third module of the paper and we will be talking about the array 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 discuss the different implementations of List ADT<\/p>\n<p>\u2022 To understand the array and array based vector implementation of Lists<\/p>\n<p>\u2022 To outline the implementation of Lists using dynamic arrays<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.1<\/strong>\u00a0<strong>Implementation of an ADT<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Before we go on to describe the implementation of a particular type of ADT let us discuss the general issues in implementing an ADT. The first step in the implementation is the choiceof the data structure to represent the ADT\u2019s data. This choice of the data structure depends mainly on details of the ADT\u2019s operations and the context in which the operations will be used. You must remember that the implementation details should be hidden behind a wall of ADT operations. In other words a program would only be able to access the data structure using the ADT operations.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The first step in implementing an ADT is choosing a <strong>data structure<\/strong>such as arrays, records, etc. to represent the ADT. Each operation associated with the ADT is implemented by one or more subroutines. Two standard implementations for the list ADT that we will be discussing in this paper are array-based implementation and linked list based implementation. In this module we will be discussing the array based implementation of the List ADT.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.2 Static Implementation of ADT List<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The simplest method to implement a List ADT is to use an array that is a\u201clinear list\u201d or a \u201ccontiguous list\u201d where elements are stored in contiguous array positions. The implementation specifies an array of a particular maximum length, and all storage is allocated before run-time. It is a sequence of n-elements where the items in the array are stored with the index of the array related to the position of the item in the list.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In array implementation,elements are stored in contiguous array positions (Figure 3.1). An array is a viable choice for storing list elements when the elements are sequential,\u00a0<span style=\"font-size: 1em;text-align: initial\">because it is a commonly available data type and in general algorithm development is easy.<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-43 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled.png\" alt=\"\" width=\"409\" height=\"253\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled.png 409w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-300x186.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-225x139.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-350x217.png 350w\" sizes=\"auto, (max-width: 409px) 100vw, 409px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>3.2.1List Implementation using arrays<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In order to implement lists using arrays we need an array for storing entries \u2013 <strong>listArray[0,1,2\u2026..m], <\/strong>a variable<strong> curr<\/strong>to keep track of the number of array elements currently assigned to the list<strong>,<\/strong> thenumber of items in the list, or current size of the list <strong>size<\/strong>and a variable to record the maximum length of the array, and therefore of the list \u2013<strong> maxsize as shown in Figure 3.2.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-44 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-1.png\" alt=\"\" width=\"604\" height=\"171\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-1.png 604w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-1-300x85.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-1-65x18.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-1-225x64.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-1-350x99.png 350w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/p>\n<p style=\"text-align: justify\">Fix one end of the list at index 0 and elements will be shifted<strong><em>as needed<\/em><\/strong> when an element is added or removed. Therefore insert and delete operations will take O(n). That is if we want to insert at position 0 (insert as first element),this requires first pushing the entire array to the right one spot to make room for the new element. Similarly deleting the element at position 0requires shifting all the elements in the list left one spot.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">One of the important operations associated with lists is the initialization. In this case we need to first declare the array of list items with maximum number of items say n. This then requires an estimate of the maximum size of the list. Once we decide the size, then we `initialize the list with number of items set at 0.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.2.1.1 Finding First Element<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here we define finding the first element by first calling a Boolean function \u2013 IsEmpty list. If IsEmpty listis true that is if the list is empty then there is no question of finding an element and finding the first element will return a value that is not a valid entry of the list However if IsEmpty listis false that is the list is not empty the element at the first location position (0) is returned.<\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a03.2.1.2 Insertion<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">What happens if you want to insert an item at a specified position in an existing array?The item originally at the given index must be moved right one position, and all the items after that index must be <strong><em>shifted right<\/em><\/strong>(Figure 3.3).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-45 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-2.png\" alt=\"\" width=\"571\" height=\"231\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-2.png 571w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-2-300x121.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-2-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-2-225x91.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-2-350x142.png 350w\" sizes=\"auto, (max-width: 571px) 100vw, 571px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us consider a specific case of Insert operation. Insert (3, K, List) In this case, 3 is the index or the point of Insertion, K is the element to be inserted &amp; List is the list in which insertion is to be done ( Figure 3.4 (a) &amp; Figure 3.4 (b)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-46 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-3.png\" alt=\"\" width=\"625\" height=\"417\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-3.png 625w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-3-300x200.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-3-65x43.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-3-225x150.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-3-350x234.png 350w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/p>\n<p style=\"text-align: justify\">Let us see the steps in the insertion with the example given in Figure 3.4(a) &amp; Figure 3.4 (b). Let us assume initially that the original list contains 7 elements (that is Size =7) namely A at array location 0, X at location 1 and so on and finally Rat location 6. The steps involved are as follows:<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">Step 1: First check whether there is place to add the new element \u2013 in other words whether the array is full. If so indicate error<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>size = maxsize \u2013 Then error<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 2: Now we need to make room for the new element at the index position 3. We need to shift elements from index to curr (curr position of last element of list) one position to the right.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Loop till curr<\/strong><\/p>\n<p><strong>Items[index+1] =&gt;<\/strong><strong>tems[index]<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Step 3: Write the element into the gap created by shifting the elements to the right<\/p>\n<p><strong>Items[index] <\/strong><strong>=&gt;<\/strong><strong> K<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Step 4: Update size and curr position of the list<\/p>\n<p><strong>size=&gt;<\/strong><strong> size +1; curr<\/strong><strong>=&gt;<\/strong><strong>curr +1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.2.2.2 Deletion from Lists<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">What happens if you want to remove an item from a specified position in an existing array? There are two ways in which this can be done:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2013\u00a0\u00a0 Leave <strong><em>gaps<\/em><\/strong> in the array, i.e. indices that contain no elements, which in practice, means that the array element has to be given a special value to indicate that it is \u201cempty\u201d, or<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2013 All the items after the (removed item\u2019s) index must be <strong><em>shifted left<\/em><\/strong>similar to what we did when we wanted to insert \u2013 only for insertion we shifted right.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2013 <strong>Delete (3, List)<\/strong>. In this case, 3 is the index or the point of Deletion&amp; List is the list from which deletion is to be done (Figure 3.5 (a) &amp; Figure 3.5 (b)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-47 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-4.png\" alt=\"\" width=\"608\" height=\"416\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-4.png 608w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-4-300x205.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-4-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-4-225x154.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-4-350x239.png 350w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">Let us see the steps in the deletion with the example given in Figure 3.5(a) &amp; Figure 3.5(b). Let us assume initially that the original list contains 7 elements (that is Size =7) namely A at array location 0, X at location 1 and so on and finally R at location 6. The steps involved are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 1: First check whether the list is empty that is whether there are elements in the list.<\/p>\n<p>We cannot perform deletion from an empty list.<\/p>\n<p><strong>size=0 Then error<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 2: Now we need to shift elements from index position 4 to the left so that an empty locationis created due to the deletion at location 3 will be filled by elements to the left. We need to shift elements from index (i) to curr( that is the curr position of last element of list) one position to the left.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Loop till curr<\/strong><\/p>\n<p><strong>Items[index] <\/strong><strong>=&gt;<\/strong><strong> items[index+1]<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Step 4: Update size and curr position of the list<\/p>\n<p><strong>size=&gt;<\/strong><strong>size-1; curr<\/strong><strong>=&gt;<\/strong><strong>curr-1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.2 Advantages of Array-Based Implementation of Lists<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Some of the major advantages of using array implementation of lists are:<\/p>\n<ul>\n<li>Array is a natural way to implement lists<\/li>\n<li>Arrays allow fast, random access of elements<\/li>\n<li style=\"text-align: justify\">Array based implementation is memory efficient since very little memory is required other than that needed to store the actual contents<\/li>\n<\/ul>\n<p>Some of the disadvantages of using arrays to implement lists are:<\/p>\n<ul>\n<li style=\"text-align: justify\">The size of the list must be known when the array is created and is fixed (static)<\/li>\n<li style=\"text-align: justify\">Array implementations of lists use a static data structure. Often defined at compile-time. This means the array size or structure cannot be altered while program is running. This requires an accurate estimate of the size of the array.<\/li>\n<li style=\"text-align: justify\">This fixing of the size beforehand usually results in overestimation of size which means we tend to usually waste space rather than have program run out.<\/li>\n<li style=\"text-align: justify\">The deletion and insertion of elements into the list is slow since it involves shifting of elements. It also means that data must be added to the end of the list for insertion and deletion to be efficient. If insertion and deletion is towards the front of the list, all other elements must shuffle down. This is slow and inefficient. This inefficiency is even more pronounced when the size of the list is large.<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 \u00a03.3 The Vector ADT<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Vector ADT extends the notion of array by storing a sequence of arbitrary objects. However instead of using position or index to access an element, an element can be accessed, inserted or removed by specifying its rank that is the number of elements preceding it. An exception will be thrown if an incorrect rank is specified (e.g. negative rank, or larger than current size). The main vector based list operations are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022\u00a0 elemAtRank(integer r): This operation returns the element at rank r without removing it.<\/p>\n<p style=\"text-align: justify\">\u2022 replaceAtRank(integer r, object o): This operation replaces the element at\u00a0<span style=\"font-size: 1em;text-align: initial\">rank with an element o and returns the old element.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u2022 insertAtRank(integer r, object o): This operation inserts a new element o so that it has rank r<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">\u2022 removeAtRank(integer r): This operation removes and returns the element at rank r Other common operations includesize() and isEmpty().<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0<\/strong><\/p>\n<p><strong>3.3.1\u00a0 Applications of Vectors<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Vectors have some specific applications. Direct applications include sorted collection of objects like in an elementary database. There are also some indirect applications such as use as an auxiliary data structure for algorithms and as components of other data structures.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.3.2 Array-based Vector<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this implementation we use an array <strong><em>V<\/em><\/strong>containing <strong><em>n<\/em><\/strong><em>elements<strong>.<\/strong><\/em> A variable <strong><em>size=n<\/em><\/strong> is used to keep track of the size of the vector (number of elements stored). Normally an array is addressed by its index and when representing the list by an array the rank and index are identical. However we differentiate the vector representation with rank to explain the important operations to be supported when using a vector.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The important operation <strong><em>elemAtRank<\/em><\/strong>(<strong><em>r<\/em><\/strong>) is implemented in <strong><em>O<\/em><\/strong>(1) time by returning <strong><em>V<\/em><\/strong>[<strong><em>r<\/em><\/strong>] (Figure 3.6). Similar to array implementation of lists, in operation <strong><em>insertAtRank<\/em><\/strong>(<strong><em>r<\/em>,<\/strong> <strong><em>o<\/em><\/strong>), we need tomake room for the new element by shifting forward the <strong><em>n<\/em><\/strong> &#8211; <strong><em>r<\/em><\/strong> elements <strong><em>V<\/em><\/strong>[<strong><em>r<\/em><\/strong>], \u2026,<strong><em>V<\/em><\/strong>[<strong><em>n<\/em><\/strong>-1] (Figure 3.7). In the worst case (<strong><em>r<\/em><\/strong> =0), this takes <strong><em>O<\/em><\/strong>(<strong><em>m<\/em><\/strong>) time<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-5.png\" alt=\"\" width=\"636\" height=\"340\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-5.png 636w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-5-300x160.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-5-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-5-225x120.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-5-350x187.png 350w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/p>\n<p style=\"text-align: justify\">Similar to array implementation of lists, in operation <strong><em>removeAtRank<\/em><\/strong>(<strong><em>r<\/em><\/strong>), we need to fill the\u00a0<span style=\"text-align: initial;font-size: 1em\">hole left by the removed element by shifting backward the <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>n<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> &#8211; <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>r<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> -1 elements <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>V<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">[<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>r<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> +1], \u2026,<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>V<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">[<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>n<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> -1] ( Figure 3.8). Again in the worst case (<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>r<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> =0), this takes <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>O<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">(<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>n<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) time.<\/span><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-49 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-6.png\" alt=\"\" width=\"608\" height=\"186\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-6.png 608w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-6-300x92.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-6-65x20.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-6-225x69.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-6-350x107.png 350w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.3 Performance<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the array based implementation of a Vector, the space used by the data structure is <strong><em>O<\/em><\/strong>(<strong><em>n<\/em><\/strong>). The operations<strong><em> size<\/em><\/strong>,<strong><em> isEmpty<\/em><\/strong>,<strong><em> elemAtRank <\/em><\/strong>and<strong><em> replaceAtRank<\/em><\/strong>all run in<strong><em> O<\/em><\/strong>(1) time. However the operations <strong><em>insertAtRank<\/em><\/strong> and <strong><em>removeAtRank<\/em><\/strong> run in <strong><em>O<\/em><\/strong>(<strong><em>n<\/em><\/strong>) time. If we use the array in a circular fashion,<strong><em>insertAtRank<\/em><\/strong>(0) and <strong><em>removeAtRank<\/em><\/strong>(0) run in <strong><em>O<\/em><\/strong>(1) time. In an <strong><em>insertAtRank<\/em><\/strong> operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.4 ADT List using Dynamic arrays<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A dynamic data structure is one that changes size, as needed, as items are inserted or removed. There is usually no limit on the size of such structures, other than the size of main memory. Dynamic arrays are arrays that grow (or shrink) as required. In other words a new array is created when the old array becomes full by creating a new array object, copying the values from the old array and then assigning the new array to the existing array reference variable<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-50 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-7.png\" alt=\"\" width=\"681\" height=\"458\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-7.png 681w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-7-300x202.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-7-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-7-225x151.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/Untitled-7-350x235.png 350w\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" \/><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">Let us see the steps in the insertion of an element into a dynamic array with the example given in Figure 3.9(a) &amp; Figure 3.9(b). Let us assume initially that the original array has a size of 6 ( 0,1 \u2026.5) and contains 4 elements <\/span><strong style=\"text-align: justify;font-size: 1em\">6,1,7,8<\/strong><span style=\"text-align: justify;font-size: 1em\"> occupying locations 0,1,2 and 3 respectively, with curr pointing to 4 the next empty location (Figure 3.9(a) \u2013 (i)). The steps involved are as follows:<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify\">\u00a0 \u00a0 Step 1: Now let us assume that we want to insert <strong>3<\/strong>. Now element 3 will be inserted in location 4 and curr will be incremented to 5 (Figure 3.9(a) \u2013 (ii)).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 2:Now let us assume that we want to insert <strong>9<\/strong>. Now element 9 will be inserted in location 5 and curr will be incremented to 6 (Figure 3.9(a) \u2013 (iii)).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 3: Now let us assume that we want to insert <strong>2<\/strong>. At this point curr&gt;maxsize of array and list full condition will be signaled as there is no room for a new item. In static array \u2013 an exception will arise.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 4: However in the case of dynamic arrays, we will create a new, bigger array as shown in Figure 3.9(b) \u2013 (i).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 5: Now all the elements of the old array will be copied to the new array and curr set to 6 (Figure 3.9(b) \u2013 (ii)).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Step 6: Now finally we can insert 2 into the new array and accordingly curr is set to 7 (Figure 3.9(b) \u2013 (iii)).<\/p>\n<p>&nbsp;<\/p>\n<p>The old array will eventually be deleted<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.4.2 Problems associated with Dynamic Array<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Insertion using dynamic array essentially means that before every insertion, we need to check if the array needs to grow. An important question that arises is that when growing an array, how much to grow it?. Is this decision based on memory efficiency that is grow by one only \u2013 since we need one location when one element is to be inserted? But in this case the time efficiency is very poor since the expensive copy operation has to be carried out every time an insertion occurs. Growing by doubling works well in practice, because it grows very large very quickly -10, 20, 40, 80, 160, 320, 640, 1280,\u2026and so on.In this case very few array re-sizingsare needed. While the copying operation in this case is again expensive, this copying does not have to be done often.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When the doubling does happen it may be time-consumingbutright after a doubling half the array is empty. However re-sizing after each insertion would be prohibitively slow. Deleting and inserting in the middle of an array (on average) is still of the order of O(n).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.4\u00a0 Summary<\/strong><\/p>\n<ul>\n<li>Explained the different implementations of Lists using arrays<\/li>\n<li>Discussed the Pros and Cons of each implementation<\/li>\n<li>Discussed the different operations possible with array based implementation of Lists<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Array Implementation of List ADT<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/HPPGQ9wj0no\" 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<p><strong style=\"font-size: 1em\">\u00a0 \u00a0 \u00a0 Web Links<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\">en.wikipedia.org\/wiki\/List_(abstract_data_type)<\/li>\n<li style=\"text-align: justify\">www.cs.cmu.edu\/~tcortina\/15110sp12\/Unit06PtA.pdf<\/li>\n<li style=\"text-align: justify\">www.csee.umbc.edu\/courses\/undergraduate\/341\/&#8230;\/Lists\/List1.html<\/li>\n<li style=\"text-align: justify\">www.doc.ic.ac.uk\/~ar3\/lectures\/ProgrammingII\/&#8230;\/Lecture2PrintOut.pdf<\/li>\n<li style=\"text-align: justify\">www.cs.sfu.ca\/CourseCentral\/225\/&#8230;notes\/dynamic-arrays-link_list.pdf<\/li>\n<li style=\"text-align: justify\">www.math.bas.bg\/~nkirov\/2009\/NETB201\/slides\/ch05\/ch05.html<\/li>\n<li style=\"text-align: justify\">www.cs.sfu.ca\/CourseCentral\/225\/amhunter\/lecturenotes\/DynamicArray.ppt<\/li>\n<li style=\"text-align: justify\">http:\/\/orion.lcg.ufrj.br\/Dr.Dobbs\/books\/book3\/chap3.htm<\/li>\n<li style=\"text-align: justify\">cs.txstate.edu\/~rp44\/cs3358_092\/Lectures\/list_revised.ppt<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Supporting &amp; Reference Materials<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Carrano and Henry, \u201cData Structures and Problem Solving with C++: Walls and Mirrors\u201d, Pearson; 6 edition, 2012<br \/>\n2. Mark Allen Weiss, \u201cData Structures and Algorithm Analysis in Java\u201d, Pearson; 3rd Edition, 2011<br \/>\n3. Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser, \u201cData Structures and Algorithms in Java\u201d, Wiley; 6 edition, 2014<\/p>\n","protected":false},"author":3,"menu_order":3,"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-40","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\/40","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":23,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/40\/revisions"}],"predecessor-version":[{"id":895,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/40\/revisions\/895"}],"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\/40\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=40"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=40"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}